iOS--将图片保存至本地相册
对方给我发一个图片,我要保存到本地,IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAlbum方法对图像进行保存,该方法会将image保存至用户的相册中:
上代码:
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
参数说明:
image : 需要保存的图片对象
id : 响应方法对象
SEL : 选择方法
步骤:
/**
* 将图片添加到本地相册
*/
- (void)addImageViewTolocal{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_currentItem.imageURL]];
UIImage *myImage = [UIImage imageWithData:data];
[self saveImageToPhotos:myImage];
}
步骤二:
- (void)saveImageToPhotos:(UIImage*)savedImage
{
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
步骤三:
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
message:msg
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
这样就可以实现将图片保存至本地相册;
今天优化上面的方法,只能保存到相册,而不能保存到指定相册,
首先需要导入系统的库:
创建一个管理类:
.h
#import#import
@class UIImage;
typedef void (^STAlbumSaveHandler)(UIImage *image, NSError *error);
/** * @brief 将图片写入相册,使用ALAssetLibrary * *
@param image 需要写入的图片 *
@param album 相册名称,如果相册不存在,则新建相册 *
@param completionHandler 回调
*/
extern void STImageWriteToPhotosAlbum(UIImage *image, NSString *album, STAlbumSaveHandler completionHandler);
@interface STAlbumManager : NSObject
+ (instancetype)sharedManager;
/** * @brief 将图片写入相册,使用ALAssetLibrary * *
@param image 需要写入的图片 *
@param album 相册名称,如果相册不存在,则新建相册 *
@param completionHandler 回调
*/
- (void)saveImage:(UIImage *)image toAlbum:(NSString *)album completionHandler:(STAlbumSaveHandler)completionHandler;
@end
@interface ALAssetsLibrary (STAssetsLibrary)
- (void)writeImage:(UIImage *)image toAlbum:(NSString *)album completionHandler:(STAlbumSaveHandler)completionHandler;
@end
.m
#import "STAlbumManager.h"
#importvoid STImageWriteToPhotosAlbum(UIImage *image, NSString *album, STAlbumSaveHandler completionHandler) {
[[STAlbumManager sharedManager] saveImage:image toAlbum:album completionHandler:completionHandler];
}
@interface STAlbumManager ()
@property(nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@end
@implementation STAlbumManager
static STAlbumManager *_sharedManager;
+ (instancetype)sharedManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ _sharedManager = [[self alloc] init]; });
return _sharedManager;
}
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (ALAssetsLibrary *)assetsLibrary {
if (!_assetsLibrary) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
}
return _assetsLibrary;
}
- (void)saveImage:(UIImage *)image toAlbum:(NSString *)album completionHandler:(STAlbumSaveHandler)completionHandler {
[self.assetsLibrary writeImage:image
toAlbum:album
completionHandler:^(UIImage *image, NSError *error) {
if (completionHandler) {
completionHandler(image, error);
}
/// 注意,这里每次都置空是因为期间如果操作相册了,下次保存之前希望能取到最新状态。
self.assetsLibrary = nil;
}];
}
@end
@implementation ALAssetsLibrary (STAssetsLibrary)
- (void)writeImage:(UIImage *)image toAlbum:(NSString *)album completionHandler:(STAlbumSaveHandler)completionHandler {
[self writeImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
if (completionHandler) {
completionHandler(image, error);
}
} else {
[self addAssetURL: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) {
[self assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
completionHandler(nil);
}
failureBlock:^(NSError *error) { completionHandler(error); }];
};
__block ALAssetsGroup *group;
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *_group, BOOL *stop) {
if ([album isEqualToString:[_group valueForProperty:ALAssetsGroupPropertyName]]) {
group = _group;
}
if (!_group) {
/// 循环结束
if (group) {
assetForURLBlock(assetURL, group);
} else {
[self addAssetsGroupAlbumWithName:album
resultBlock:^(ALAssetsGroup *group) { assetForURLBlock(assetURL, group); }
failureBlock:completionHandler];
}
}
}
failureBlock:completionHandler];
}
@end
在需要的地方调用就可以实现了...