欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

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

时间:2023-02-19 10:52:59|栏目:iOS代码|点击:

实例如下所示:

- (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基于UITableView实现多层展开与收起

栏    目:iOS代码

下一篇:没有了

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

本文地址:http://www.codeinn.net/misctech/226027.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有