我正在尝试将视频文件从应用程序的文档文件夹保存到设备的相机胶卷。 albumChangeRequest 尝试将资产复制到相机胶卷时发生错误。它无法将文件复制到相册,并返回错误消息(Domain = NSCocoaErrorDomain Code = -1“(null)”,操作无法完成)。我以前使用过不推荐的方法( writeVideoAtPathToSavedPhotosAlbum )来复制视频,它也使用了相同的文件路径,所以问题不应该是文件路径网址。
...
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];
// Handle the result asset id in urlSubStr.
} else {
// Copy failed.
}
}];
}
...
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 {
// Copy failed
}
})
}
}