有一種方法可以做到這一點。
目標C
導入照片框架
#import <Photos/Photos.h>
然後,讓你的形象
UIImage * image = [UIImage imageNamed:@"DP.jpg"];
然後保存圖像。該圖像將被保存到相機膠捲。
這將工作一樣writeImageToSavedPhotosAlbum
的ALAssetsLibrary
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"Image Saved");
}
else {
NSLog(@"error in saving image : %@",error);
}
}];
確保照片的權限是ON。 此外,如果你已經創建的Xcode 8的項目,那麼你就需要在Info.plist
<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>
添加鍵,您也可以使用creationRequestForAssetFromImage
方法不僅使UIImage的,也有圖片的網址了。
這些是您可以使用的方法從照片框架。
+ (instancetype)creationRequestForAssetFromImage:(UIImage *)image;
+ (nullable instancetype)creationRequestForAssetFromImageAtFileURL:(NSURL *)fileURL;
+ (nullable instancetype)creationRequestForAssetFromVideoAtFileURL:(NSURL *)fileURL;
雨燕3.0
let image = UIImage(named: "DP.jpg")
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image!)
}) { (success:Bool, error:Error?) in
if success {
print("Image Saved Successfully")
} else {
print("Error in saving:"+error.debugDescription)
}
}
其他方法照片框架爲雨燕是:
open class func creationRequestForAsset(from image: UIImage) -> Self
open class func creationRequestForAssetFromImage(atFileURL fileURL: URL) -> Self?
open class func creationRequestForAssetFromVideo(atFileURL fileURL: URL) -> Self?
試試這個http://stackoverflow.com/問題/ 37405691 /更換-alassertlibrary叔o-get-the-filename/37405786#37405786 –
[如何將圖片保存到iPhone照片庫?](http://stackoverflow.com/questions/178915/how-to-save-picture-to-iphone - 照片庫) –
不,這個問題和答案不同 – kenzolek