欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现图片保存与搜索功能

程序员文章站 2023-01-01 18:17:28
本文实现photos.framework的常见功能,包括:创建自定义相册、保存图片到自定义相册、搜索所有相册的图片,具体内容如下 1、保存图片到相册 /*...

本文实现photos.framework的常见功能,包括:创建自定义相册、保存图片到自定义相册、搜索所有相册的图片,具体内容如下
1、保存图片到相册

/**
 * 保存图片到相册
 */
- (ibaction)saveimage {
  phauthorizationstatus status = [phphotolibrary authorizationstatus];
  if (status == phauthorizationstatusdenied) {
    nslog(@"请到【设置-隐私-照片】打开访问开关");
  } else if (status == phauthorizationstatusrestricted) {
    nslog(@"无法访问相册");
  } else {
    // 保存相片的标识
    __block nsstring *assetid = nil;
       
    [[phphotolibrary sharedphotolibrary] performchanges:^{
      // 保存相片到相机胶卷,并返回标识
      assetid = [phassetcreationrequest creationrequestforassetfromimage:[uiimage imagenamed:@"logo"]].placeholderforcreatedasset.localidentifier;
    } completionhandler:^(bool success, nserror * _nullable error) {
      if (!success) {
        nslog(@"保存失败:%@", error);
        return;
      }
         
      // 根据标识获得相片对象
      phasset *asset = [phasset fetchassetswithlocalidentifiers:@[assetid] options:nil].lastobject;
         
      // 拿到自定义的相册对象
      phassetcollection *collection = [self collection];
      if (collection == nil) return;
         
      // 保存相片到自定义相册中
      [[phphotolibrary sharedphotolibrary] performchanges:^{
        [[phassetcollectionchangerequest changerequestforassetcollection:collection] addassets:@[asset]];
      } completionhandler:^(bool success, nserror * _nullable error) {
        if (success) {
          nslog(@"保存成功");
        } else {
          nslog(@"保存失败:%@", error);
        }
      }];
    }];
  }
}

2、搜索所有相片

/**
 * 查询所有的图片
 */
- (ibaction)searchallimages {
     
  // 遍历所有的自定义相册
  phfetchresult<phassetcollection *> *collectionresult0 = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype:phassetcollectionsubtypealbumregular options:nil];
  for (phassetcollection *collection in collectionresult0) {
    [self searchallimagesincollection:collection];
  }
     
  // 获得相机胶卷的图片
  phfetchresult<phassetcollection *> *collectionresult1 = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum subtype:phassetcollectionsubtypealbumregular options:nil];
  for (phassetcollection *collection in collectionresult1) {
    if (![collection.localizedtitle isequaltostring:@"camera roll"]) continue;
    [self searchallimagesincollection:collection];
    break;
  }
}
   
/**
 * 查询某个相册里面的所有图片
 */
- (void)searchallimagesincollection:(phassetcollection *)collection
{
  // 采取同步获取图片(只获得一次图片)
  phimagerequestoptions *imageoptions = [[phimagerequestoptions alloc] init];
  imageoptions.synchronous = yes;
     
  nslog(@"相册名字:%@", collection.localizedtitle);
     
  // 遍历这个相册中的所有图片
  phfetchresult<phasset *> *assetresult = [phasset fetchassetsinassetcollection:collection options:nil];
  for (phasset *asset in assetresult) {
    // 过滤非图片
    if (asset.mediatype != phassetmediatypeimage) continue;
       
    // 图片原尺寸
    cgsize targetsize = cgsizemake(asset.pixelwidth, asset.pixelheight);
    // 请求图片
    [[phimagemanager defaultmanager] requestimageforasset:asset targetsize:targetsize contentmode:phimagecontentmodedefault options:imageoptions resulthandler:^(uiimage * _nullable result, nsdictionary * _nullable info) {
      nslog(@"图片:%@", result);
    }];
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。