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

内容摘要
本文实现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);
    }];
  }
}

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


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!