I'm trying to save a UIImage to the camera roll.
Apple made the UIImageWriteToSavedPhotosAlbum
deprecated, therefore I'm avoiding using that (same for ALAssets
), which forces to use PhotoLibrary
.
This is the code I'm using:
definition:
var rollCollection : PHAssetCollection!;
Initialization:
let result = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil);
rollCollection = result.firstObject as? PHAssetCollection;
Code to save the picture:
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);
});
}
Every time I am trying to save an image I get the following error:
Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
Any ideas? I couldn't find any explanation, and everywhere I looked for I found a code snippet similar to mine (including Apple's documentation: https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/)
Thanks.