UIImage* image = [UIImageimageNamed:@"IMG_20160610_210557.jpg"];
[selfsaveToAlbumWithMetadata:nilimageData: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 = [[ALAssetsLibraryalloc]init];
__weakALAssetsLibrary*weakSelf = assetsLibrary;
void(^AddAsset)(ALAssetsLibrary*,NSURL*) = ^(ALAssetsLibrary*assetsLibrary,NSURL*assetURL) {
[assetsLibraryassetForURL:assetURLresultBlock:^(ALAsset*asset) {
[assetsLibraryenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup*group,BOOL*stop) {
if([[groupvalueForProperty:ALAssetsGroupPropertyName]isEqualToString:customAlbumName]) {
[groupaddAsset:asset];
if(completionBlock) {
completionBlock();
}
}
}failureBlock:^(NSError*error) {
if(failureBlock) {
failureBlock(error);
}
}];
}failureBlock:^(NSError*error) {
if(failureBlock) {
failureBlock(error);
}
}];
};
[assetsLibrarywriteImageDataToSavedPhotosAlbum:imageDatametadata:metadatacompletionBlock:^(NSURL*assetURL,NSError*error) {
if(customAlbumName) {
[assetsLibraryaddAssetsGroupAlbumWithName:customAlbumNameresultBlock:^(ALAssetsGroup*group) {
if(group) {
[weakSelfassetForURL:assetURLresultBlock:^(ALAsset*asset) {
[groupaddAsset:asset];
if(completionBlock) {
completionBlock();
}
}failureBlock:^(NSError*error) {
if(failureBlock) {
failureBlock(error);
}
}];
}else{
AddAsset(weakSelf, assetURL);
}
}failureBlock:^(NSError*error) {
AddAsset(weakSelf, assetURL);
}];
}else{
if(completionBlock) {
completionBlock();
}
}
}];
}