A.Vijay
A.Vijay

Reputation: 21

iOS 9 PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL: fails with error code -1

I'm using below code to save a video to photo library.

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);
        }
    }];

This code works well with iOS 8.x. But it fails with error code -1 in iOS 9.x. The error is

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

Any idea about this behavior in iOS 9.? Any help is appreciated ! Thanks

Upvotes: 1

Views: 1128

Answers (1)

duhao68
duhao68

Reputation: 11

You can only save in your own album You need to create a photo album

//获取自定义相册
- (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;
}

Upvotes: 1

Related Questions