iOS 保存图片到相册
iOS 保存图片到相册
//方法调用
NSMutableData *data = [NSMutableData dataWithContentsOfFile:_SaveFilepath];
UIImage *myImage = [UIImage imageWithData:data];
STAlbumManager *stabumManager = [STAlbumManager sharedManager];
[stabumManager saveImage:myImage toAlbum:@"电子发票" completionHandler:^(UIImage *image, NSError *error){}];
//类实现
//
// STAlbumManager.h
// BSbracelet
//
// Created by Christopher on 17/4/28.
// Copyright ? 2017年 ZTracy. All rights reserved.
//
#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
//
// STAlbumManager.m
// BSbracelet
//
// Created by Christopher on 17/4/28.
// Copyright ? 2017年 ZTracy. All rights reserved.
//
#import "STAlbumManager.h"
#import
void 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
上一篇: iOS 识别二维码及描绘二维码边框
下一篇: 网站入侵渗透测试的正确知识