美文网首页
iOS 保存图片到相册,一次保存多张

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

作者: 114105lijia | 来源:发表于2019-11-20 11:16 被阅读0次

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

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

    相关文章

      网友评论

          本文标题:iOS 保存图片到相册,一次保存多张

          本文链接:https://www.haomeiwen.com/subject/zmxoictx.html