首页 > 系统 > iOS > 正文

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

2020-02-19 15:50:55
字体:
来源:转载
供稿:网友

搜索框的使用在开发过程非常常见,现在几乎所有的应用程序都有搜索功能,下面是iOS图片保存与搜索功能的代码实现,跟着武林技术频道小编带你来了解一下吧!
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 *collectionResult0 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];  for (PHAssetCollection *collection in collectionResult0) {    [self searchAllImagesInCollection:collection];  }       // 获得相机胶卷的图片  PHFetchResult *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 *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);    }];  }}

以上代码都是武林技术频道小编为大家搜集的iOS图片保存与搜索功能的代码实现,希望对大家的学习有一定的帮助!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表