iOS 保存图片到相册,一次保存多张

2019-11-20  本文已影响0人  114105lijia

在网上搜索到保存图片到指定相册代码:

- (void)saveImage:(UIImage *)image imageUrl:(NSString *)imgUrl saveImageBlock:(nonnull SaveImageBlock)saveImageBlock{
    //标识保存到系统相册中的标识
    __block NSString *localIdentifier;
    
    MINISOWeakSelf;
    //首先获取相册的集合
    PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
    //对获取到集合进行遍历
    [collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        PHAssetCollection *assetCollection = obj;
        //Camera Roll是我们写入照片的相册
        if ([assetCollection.localizedTitle isEqualToString:weakSelf.folderName])  {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                //请求创建一个Asset
                PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                //请求编辑相册
                PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                //为Asset创建一个占位符,放到相册编辑请求中
                PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset];
                //相册中添加照片
                [collectonRequest addAssets:@[placeHolder]];
                
                localIdentifier = placeHolder.localIdentifier;
            } completionHandler:^(BOOL success, NSError *error) {
                if (success) {
                    saveImageBlock(YES);
                    NSLog(@"保存图片成功!");
                    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self readFromPlist]];
                    [dict setObject:localIdentifier forKey:[self showFileNameFromPath:imgUrl]];
                    [weakSelf writeDicToPlist:dict];
                } else {
                    saveImageBlock(NO);
                    NSLog(@"保存图片失败:%@", error);
                }
            }];
        }
    }];
}

有时候会出现每次保存多张的问题;仔细打断点查看了下代码,发觉[collectonResuts enumerateObjectsUsingBlock:这个遍历,会把同一相册遍历多遍,所以造成保存多张图片到相册的问题。解决方法是,当遍历到指定相册时,停止遍历,添加 *stop = YES;如下:

- (void)saveImage:(UIImage *)image imageUrl:(NSString *)imgUrl saveImageBlock:(nonnull SaveImageBlock)saveImageBlock{
    //标识保存到系统相册中的标识
    __block NSString *localIdentifier;
    
    MINISOWeakSelf;
    //首先获取相册的集合
    PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
    //对获取到集合进行遍历
    [collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        PHAssetCollection *assetCollection = obj;
        //Camera Roll是我们写入照片的相册
        if ([assetCollection.localizedTitle isEqualToString:weakSelf.folderName])  {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                //请求创建一个Asset
                PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                //请求编辑相册
                PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                //为Asset创建一个占位符,放到相册编辑请求中
                PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset];
                //相册中添加照片
                [collectonRequest addAssets:@[placeHolder]];
                
                localIdentifier = placeHolder.localIdentifier;
            } completionHandler:^(BOOL success, NSError *error) {
                if (success) {
                    saveImageBlock(YES);
                    NSLog(@"保存图片成功!");
                    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self readFromPlist]];
                    [dict setObject:localIdentifier forKey:[self showFileNameFromPath:imgUrl]];
                    [weakSelf writeDicToPlist:dict];
                } else {
                    saveImageBlock(NO);
                    NSLog(@"保存图片失败:%@", error);
                }
            }];
            //重要,可能遍历多遍,当找到对应的相册时,就要停止循环
            *stop = YES;
        }
    }];
}
上一篇下一篇

猜你喜欢

热点阅读