Apple不推荐使用UIImageWritetoSavedPhotosAlbum,因此我避免使用它(对于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!,completion: nil); } }); }) } else { dispatch_async(dispatch_get_main_queue(),{ self.presentVIEwController(self.alertCantSave!,completion: nil); }); }
每次我尝试保存图像时,都会出现以下错误:
Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
有任何想法吗?
我找不到任何解释,在我找到的任何地方,我都找到了类似于我的代码片段(包括Apple的文档:https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/)
谢谢.
解决方法 我在寻找完全相同的事情几个小时后才发现这个问题.玩了一下我能够得到一个对我有用的答案,所以我在这里分享它,以防万一其他人在将来寻找类似的东西.
// Make sure the image is not nilguard let image = image else { // No image here... return}// Perform changes to the library PHPhotolibrary.sharedPhotolibrary().performChanges({ // You only need to create the request,there is no need to define the placeholder // nor to add the asset because swift does not like it when you try to modify the camera roll. PHAssetChangeRequest.creationRequestForAssetFromImage(image)},error in // Same as what you have...})
如果您想要一个如何将图像保存到自定义相册并从自定义相册查询图像的示例,请查看此答案. Save images with PHImageManager
总结以上是内存溢出为你收集整理的ios – 未能使用PHPhotoLibrary将UIImage保存到相机胶卷全部内容,希望文章能够帮你解决ios – 未能使用PHPhotoLibrary将UIImage保存到相机胶卷所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)