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

Posted

技术标签:

【中文标题】将图像保存到特定相册,但限制保存到相机胶卷文件夹【英文标题】:Save image to a specific photo album, but restrict to save into camera roll folder 【发布时间】:2019-02-11 09:36:58 【问题描述】:

我需要一点帮助,我有一个方法可以将 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】:

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

【讨论】:

以上是关于将图像保存到特定相册,但限制保存到相机胶卷文件夹的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中将照片保存在特定相册中

从图像创建视频并保存到相册

自定义相册中的图像保存问题以及 iphone 中的默认相机胶卷

Phonegap(cordova)将图像保存到自定义相册

如何避免将从相机胶卷中挑选的图像保存到相机胶卷? [iOS]

从 iOS 中的相机胶卷中获取特定图像