将图像保存到特定的相册,但限制保存到相机胶卷文件夹中

时间:2019-02-11 09:36:58

标签: ios objective-c phphotolibrary

在这里我需要一点帮助,我有一种方法可以将UIImage保存到我的自定义图像文件夹中,但是图像也保存到相机胶卷文件夹中,这是我不想要的。

请,有人可以建议我如何限制保存到相机胶卷文件夹中吗?

NSString *albumName = @"myApp";
void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
        [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];

    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating asset: %@", error);
        }
    }];
};

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName];
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];

if (fetchResult.count > 0) {
    saveBlock(fetchResult.firstObject);
} else {
    __block PHObjectPlaceholder *albumPlaceholder;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
        albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;

    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
            if (fetchResult.count > 0) {
                saveBlock(fetchResult.firstObject);
            }
        } else {
            NSLog(@"Error creating album: %@", error);
        }
    }];
}
NSLog(@"Count : %lu",(unsigned long)[arrForHereLoImages count]);

1 个答案:

答案 0 :(得分:0)

相机胶卷包含所有设备的图片。这是iOS设计的。你无法避免。
您的图像不会保存两次,而是实际保存在相机胶卷中,并在您的自定义相册中引用。