iOS開發-新建相簿並儲存圖片到該相簿

CocoaJason發表於2018-04-14
UIImage* image = [UIImage imageNamed:@"IMG_20160610_210557.jpg"];
    [self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(image) customAlbumName:@"iOSDevTip" completionBlock:^{
        
    } failureBlock:^(NSError *error) {
        
    }];
複製程式碼
- (void)saveToAlbumWithMetadata:(NSDictionary*)metadata
                      imageData:(NSData*)imageData
                customAlbumName:(NSString*)customAlbumName
                completionBlock:(void(^)(void))completionBlock
                   failureBlock:(void(^)(NSError*error))failureBlock
{
    
    ALAssetsLibrary*assetsLibrary = [[ALAssetsLibrary alloc]init];
    __weak ALAssetsLibrary*weakAssetsLibrary = assetsLibrary;
    
    void(^AddAsset)(ALAssetsLibrary*,NSURL*) = ^(ALAssetsLibrary*assetsLibrary,NSURL*assetURL) {
        [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset*asset) {
            [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup*group,BOOL*stop) {
                
                if([[group valueForProperty:ALAssetsGroupPropertyName]isEqualToString:customAlbumName]) {
                    
                    [group addAsset:asset];
                    
                    if(completionBlock) {
                        
                        completionBlock();
                        
                    }
                    
                }
                
            }failureBlock:^(NSError*error) {
                
                if(failureBlock) {
                    
                    failureBlock(error);
                    
                }
                
            }];
            
        }failureBlock:^(NSError*error) {
            
            if(failureBlock) {
                
                failureBlock(error);
                
            }
            
        }];
        
    };
    
    [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData  metadata:metadata completionBlock:^(NSURL*assetURL,NSError*error) {
        
        if(customAlbumName) {
            
            [assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup*group) {
                
                if(group) {
                    
                    [weakAssetsLibrary assetForURL:assetURL resultBlock:^(ALAsset*asset) {
                        
                        [group addAsset:asset];
                        
                        if(completionBlock) {
                            
                            completionBlock();
                            
                        }
                        
                    }failureBlock:^(NSError*error) {
                        
                        if(failureBlock) {
                            
                            failureBlock(error);
                            
                        }
                        
                    }];
                    
                }else{
                    
                    AddAsset(weakAssetsLibrary, assetURL);
                    
                }
                
            }failureBlock:^(NSError*error) {
                
                AddAsset(weakAssetsLibrary, assetURL);
                
            }];
            
        }else{
            
            if(completionBlock) {
                
                completionBlock();
                
            }
            
        }
        
    }];
    
}
複製程式碼

相關文章