我正在尝试将 UIImage 保存到相机胶卷。AppleUIImageWriteToSavedPhotosAlbum
已弃用,因此我避免使用它(同样适用于ALAssets
),它强制使用PhotoLibrary
.
这是我正在使用的代码:
定义:
var rollCollection : PHAssetCollection!;
初始化:
let result = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil);
rollCollection = result.firstObject as? PHAssetCollection;
保存图片的代码:
if (rollCollection != nil){
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(
self.originalImg!);
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.rollCollection!);
let assetPlaceHolder = assetRequest.placeholderForCreatedAsset;
albumChangeRequest!.addAssets([assetPlaceHolder!])
}, completionHandler: { success, error in
dispatch_async(dispatch_get_main_queue(), {
print ("\(error!)");
if (!success){
self.presentViewController(self.alertCantSave!, animated: false, completion: nil);
}
else {
self.presentViewController(self.alertSaved!, animated: false, completion: nil);
}
});
})
}
else {
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(self.alertCantSave!, animated: false, completion: nil);
});
}
每次我尝试保存图像时,都会出现以下错误:
Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
有任何想法吗?我找不到任何解释,在我寻找的所有地方,我都发现了一个类似于我的代码片段(包括 Apple 的文档:https ://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/ )
谢谢。