保存照片到系统相册
保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album)。
1.创建UIImageView
创建UIImageView是为了将照片展示出来,我们是要把UIImage保存到系统相册(Photo Album):
self.image = [UIImage imageNamed:@"iOSDevTip"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN.width - 300) / 2, 70, 300, 150)];
imageView.image = self.image;
[self.view addSubview:imageView];
2.创建UIButton
创建UIButton并绑定actionClick:事件:
- (void)actionClick:(UIButton *)button
{
}
3.保存照片到系统相册(Photo Album)
在actionClick:方法里调用:
保存照片到自己创建的相簿
接下来,我们来详细讲解一下关于系统相册权限获取、保存照片、创建自己的相簿等等功能。
1.创建自己的相簿
这也是一种比较创建的作法,创建自己的相簿,然后把照片或者视频保存到自己的相簿中。相关代码如下:
//创建相簿成功
} failureBlock:^(NSError *error) {
//失败
}];
这个方法也是将照片保存到系统相簿里面,不是保存到自己创建的相簿里面。代码如下:
}
}];
(1)创建相簿失败
(2)保存照片失败
在上面两个方法创建自己的相簿和保存照片的失败结果里,我们可以弹出获取照片权限失败的提示。我们拿第一个创建相簿失败来举例:
//创建相簿成功
} failureBlock:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存储失败"
message:@"请打开 设置-隐私-照片 来进行设置"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
}];
4.保存照片到自己的相簿
下面这段代码是在大谷歌里面找到的,亲自测试过是可以用的,整理如下:
else
{
BOOL haveHDRGroup = NO;
for (ALAssetsGroup *gp in groups)
{
NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"iOSDevTip"])
{
haveHDRGroup = YES;
}
}
if (!haveHDRGroup)
{
//do add a group named "XXXX"
[assetsLibrary addAssetsGroupAlbumWithName:@"iOSDevTip"
resultBlock:^(ALAssetsGroup *group)
{
[groups addObject:group];
}
failureBlock:nil];
haveHDRGroup = YES;
}
}
};
//创建相簿
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];
[self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(self.image) customAlbumName:@"iOSDevTip" completionBlock:^
{
//这里可以创建添加成功的方法
}
failureBlock:^(NSError *error)
{
//处理添加失败的方法显示alert让它回到主线程执行,不然那个框框死活不肯弹出来
dispatch_async(dispatch_get_main_queue(), ^{
//添加失败一般是由用户不允许应用访问相册造成的,这边可以取出这种情况加以判断一下
if([error.localizedDescription rangeOfString:@"User denied access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil];
[alert show];
}
});
}];
}
- (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 *weakSelf = 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) {
[weakSelf assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[group addAsset: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();
}
}
}];
}
新闻热点
疑难解答