iOS添加图片到相册

添加资源到相册中是比较常见的需求,这份代码是给一些有这方面需求,但是又比较懒,不想看Photos框架的同学们准备的.

注:以下代码均未封装,可以直接copy使用.
  • 需求一:直接添加图片到相机胶卷
// 写入图片到相机胶卷
- (void)writeImageToAssetsLibrary:(UIImage *)image {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *creationRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        PHFetchResult *fetchreult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
        PHAssetCollection *assetCollection = fetchreult.firstObject;
        PHAssetCollectionChangeRequest *addAssetRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
        PHObjectPlaceholder * assetPlaceholder = creationRequest.placeholderForCreatedAsset;
        [addAssetRequest addAssets:@[assetPlaceholder]];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {

    }];
}
  • 需求二:写入图片到指定相册,如果没有,则创建一个
- (void)writeImageWithoutWatermark:(UIImage *)image albumTitle:(NSString *)albumTitle completion:(FCHPhotoSaveCompletion)completion {

    if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
        __block PHAssetCollection *assetAlbum = nil;
        
        if (albumTitle.length == 0 && !albumTitle) {
            // 相册名为空或者长度错误直接保存系统相册
            PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
            
            assetAlbum = [result firstObject];
        }
        else {
            PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
            [result enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                // 相册是否存在
                if ([albumTitle isEqualToString:obj.localizedTitle]) {
                    assetAlbum = obj;
                    
                    *stop = YES;
                }
            }];
            
            if (!assetAlbum) {
                // 不存在创建相册
                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumTitle];
                } completionHandler:^(BOOL success, NSError * _Nullable error) {
                    // 相册创建成功保存图片
                   [self writeImageWithoutWatermark:image albumTitle:albumTitle completion:completion];
                }];
            }
        }
        
        // 保存逻辑
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
            PHObjectPlaceholder *assetPlaceholder = changeRequest.placeholderForCreatedAsset;
            PHAssetCollectionChangeRequest *albumChangeRequset = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetAlbum];
            [albumChangeRequset addAssets:@[assetPlaceholder]];
        } completionHandler:completion];
    }
    else {
        completion(NO, [NSError errorWithDomain:@"Write Album Error" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey: @"Authorization Failure"}]);
    }
}

如果对Photos感兴趣,请进一步阅读:

  • 官方Demo
  • 照片框架
  • Photos 框架实践以及坑

你可能感兴趣的:(iOS添加图片到相册)