I am trying to save a video file from the application’s document folder to the Camera Roll device. The error occurs when albumChangeRequest tries to copy a resource to Camera Roll. It cannot copy the file to the album and returns an error message (Domain = NSCocoaErrorDomain Code = -1 "(null)", the operation cannot be completed). Earlier, I used the legacy method (writeVideoAtPathToSavedPhotosAlbum) to copy the video, and also used the same file path, so the problem should not be the URL of the file path.
... PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary]; NSURL *filePathURL = [NSURL fileURLWithPath:videoPath isDirectory:NO]; __block NSString* assetURL = nil; PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil]; PHAssetCollection* album = fetchResult.firstObject; if (album) { [photoLibrary performChanges:^{ PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:filePathURL]; PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album]; PHObjectPlaceholder *assetChangePlaceHolder = assetChangeRequest.placeholderForCreatedAsset; assetURL = [assetChangePlaceHolder localIdentifier]; [albumChangeRequest addAssets:@[assetChangePlaceHolder]]; } completionHandler:^(BOOL success, NSError * error) { if ( success ){ NSString* urlSubStr = [assetURL substringToIndex:36];
Swift 3.0
let photoLibrary = PHPhotoLibrary.shared() let filePathURL = URL(fileURLWithPath: videoPath, isDirectory: false) var assetURL: String! = nil let fetchResult = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.smartAlbumUserLibrary, options: nil) if let album = fetchResult.firstObject { photoLibrary.performChanges({ if let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputFileURL), let albumChangeRequest = PHAssetCollectionChangeRequest(for: album), let assetChangePlaceholder = assetChangeRequest.placeholderForCreatedAsset { assetURL = assetChangePlaceholder.localIdentifier albumChangeRequest.addAssets(assetChangePlaceholder as! NSFastEnumeration) } }, completionHandler: { (succeed, error) in if succeed { let index = assetURL.index(assetURL.startIndex, offsetBy: 36) let urlSubStr = assetURL.substring(to: index) } else {
source share