iOS 保存图片在手机
上个项目中有个保存图片在手机相册,同时创建相册.
由于我不想写注释 直接上代码了0.0
.h
#include
@classUIImage;
typedefvoid(^STAlbumSaveHandler)(UIImage*image,NSError*error);
/**
*@brief将图片写入相册,使用ALAssetLibrary
*
*@paramimage需要写入的图片
*@paramalbum相册名称,如果相册不存在,则新建相册
*@paramcompletionHandler 回调
*/
externvoidSTImageWriteToPhotosAlbum(UIImage*image,NSString*album,STAlbumSaveHandlercompletionHandler);
@interfaceSTAlbumManager :NSObject
+ (instancetype)sharedManager;
/**
*@brief将图片写入相册,使用ALAssetLibrary
*
*@paramimage需要写入的图片
*@paramalbum相册名称,如果相册不存在,则新建相册
*@paramcompletionHandler 回调
*/
- (void)saveImage:(UIImage*)image toAlbum:(NSString*)album completionHandler:(STAlbumSaveHandler)completionHandler;
@end
@interfaceALAssetsLibrary (STAssetsLibrary)
- (void)writeImage:(UIImage*)image toAlbum:(NSString*)album completionHandler:(STAlbumSaveHandler)completionHandler;
@end
.m
#import
voidSTImageWriteToPhotosAlbum(UIImage*image,NSString*album,STAlbumSaveHandlercompletionHandler) {
[[STAlbumManagersharedManager]saveImage:imagetoAlbum:albumcompletionHandler:completionHandler];
}
@interfaceSTAlbumManager()
@property(nonatomic,strong)ALAssetsLibrary*assetsLibrary;
@end
@implementationSTAlbumManager
staticSTAlbumManager*_sharedManager;
+ (instancetype)sharedManager {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{_sharedManager= [[selfalloc]init]; });
return_sharedManager;
}
- (instancetype)init {
self= [superinit];
if(self) {
}
returnself;
}
- (ALAssetsLibrary*)assetsLibrary {
if(!_assetsLibrary) {
_assetsLibrary= [[ALAssetsLibraryalloc]init];
}
return_assetsLibrary;
}
- (void)saveImage:(UIImage*)image toAlbum:(NSString*)album completionHandler:(STAlbumSaveHandler)completionHandler {
[self.assetsLibrarywriteImage:image
toAlbum:album
completionHandler:^(UIImage*image,NSError*error) {
if(completionHandler) {
completionHandler(image, error);
}
/// 注意,这里每次都置空是因为期间如果操作相册了,下次保存之前希望能取到最新状态。
self.assetsLibrary=nil;
}];
}
@end
@implementationALAssetsLibrary (STAssetsLibrary)
- (void)writeImage:(UIImage*)image toAlbum:(NSString*)album completionHandler:(STAlbumSaveHandler)completionHandler {
[selfwriteImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL*assetURL,NSError*error) {
if(error) {
if(completionHandler) {
completionHandler(image, error);
}
}else{
[selfaddAssetURL:assetURL
toAlbum:album
completionHandler:^(NSError*error) {
if(completionHandler) {
completionHandler(image, error);
}
}];
}
}];
}
- (void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)album completionHandler:(ALAssetsLibraryAccessFailureBlock)completionHandler {
void(^assetForURLBlock)(NSURL*,ALAssetsGroup*) = ^(NSURL*URL,ALAssetsGroup*group) {
[selfassetForURL:assetURL
resultBlock:^(ALAsset*asset) {
[groupaddAsset:asset];
completionHandler(nil);
}
failureBlock:^(NSError*error) { completionHandler(error); }];
};
__blockALAssetsGroup*group;
[selfenumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup*_group,BOOL*stop) {
if([albumisEqualToString:[_groupvalueForProperty:ALAssetsGroupPropertyName]]) {
group = _group;
}
if(!_group) {
/// 循环结束
if(group) {
assetForURLBlock(assetURL, group);
}else{
[selfaddAssetsGroupAlbumWithName:album
resultBlock:^(ALAssetsGroup*group) { assetForURLBlock(assetURL, group); }
failureBlock:completionHandler];
}
}
}
failureBlock:completionHandler];
}
@end