1

我正在使用以下代码将视频保存到照片库。

NSString *ss = [finalVideoURL path];

    __block PHObjectPlaceholder *placeholder;


    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:ss]];
        _placeholder = [createAssetRequest placeholderForCreatedAsset];

    } completionHandler:^(BOOL success, NSError *error) {
        if (success)
        {

            NSLog(@" success iOS 9");
        }
        else
        {
            NSLog(@"%@", error);
        }
    }];

此代码适用于iOS 8.x. 但它失败,错误代码为 -1 in iOS 9.x。错误是

`Error Domain=NSCocoaErrorDomain Code=-1 "(null)"`

对 iOS 9 中的这种行为有任何想法吗?任何帮助表示赞赏!谢谢

4

1 回答 1

1

您只能保存在自己的相册中 您需要创建相册

//获取自定义相册
- (PHAssetCollection *)getAPPCollection {
// Getting the app album if any: 找是否已经创建自定义相册
PHFetchResult<PHAssetCollection *> *collectionResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
for (PHAssetCollection *collection in collectionResult) {
    if ([collection.localizedTitle isEqualToString:appName]) {
        return collection;
    }
}

// Creating the album: 新建自定义相册
__block NSString *collectionId = nil;
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
    collectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:appName].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error];
if (error) {
    NSLog(@"创建相册:%@失败", appName);
    return nil;
}
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionId] options:nil].lastObject;
}
于 2016-09-06T09:50:25.257 回答