iOS 把图片保存到相册,并获取图片文件名的实例

发布时间:2020-09-18 09:09:14 作者:编织人生_程就未来
来源:脚本之家 阅读:155

实例如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
 if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
 }
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
 if (error) {
  DLog(@"图片保存失败!");
 }else{
  DLog(@"图片保存成功!");
 }
}
此方法可以把其他来源的图片也可保存到相册

 //当image从相机中获取的时候存入相册中
 //保存方法1
//  UIImageWriteToSavedPhotosAlbum(img.image,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
//   imageData = nil;
  
  WEAKSELF
  //保存方法2同时获取保存的相片文件名
  __blockALAssetsLibrary *lib = [[ALAssetsLibraryalloc] init];
  [lib writeImageToSavedPhotosAlbum:img.image.CGImagemetadata:nilcompletionBlock:^(NSURL *assetURL,NSError *error) {
   
   ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
   {
    ALAssetRepresentation *imageRep = [imageAssetdefaultRepresentation];
    weakSelf.imageName = [NSStringstringWithFormat:@"%@",[imageRepfilename]];
    NSLog(@"[imageRep filename 1] : %@", [imageRepfilename]);
    DBLog(@"self.addressImageName照相0 = %@",weakSelf.imageName);
    commitBlock(img.image,UIImageJPEGRepresentation(img.image,0.5),self.imageName);
   };
   ALAssetsLibrary* assetslibrary = [[ALAssetsLibraryalloc] init];
   [assetslibrary assetForURL:assetURLresultBlock:resultblock failureBlock:nil];
   
   NSLog(@"assetURL = %@, error = %@", assetURL, error);
   lib = nil;
   
  }];
  
 }];

以上这篇iOS 把图片保存到相册,并获取图片文件名的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

推荐阅读:
  1. IOS获取图片音频方法----UIImagePickerController
  2. iOS如何实现图片压缩、滤镜、剪切及渲染

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios 图片 保存

上一篇:Linux下nginx生成日志自动切割的实现方法

下一篇:Android实现动态改变shape.xml中图形的颜色

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》