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文章

iOS开发之手势识别

2020-1-7 14:38:44

iOS文章

IOS开发利用系统自带定位

2020-1-7 17:51:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索