iOS image saving and search function
- 2020-05-15 02:12:57
- OfStack
This article implements the common functions of Photos.framework, including: creating custom albums, saving pictures to custom albums, and searching for pictures of all albums. The details are as follows
1. Save the picture to the album
/**
* Save the picture to the album
*/
- (IBAction)saveImage {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusDenied) {
NSLog(@" Please go to Settings - privacy - Photo: turn on the access switch ");
} else if (status == PHAuthorizationStatusRestricted) {
NSLog(@" Unable to access album ");
} else {
// Save photo identification
__block NSString *assetId = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Save the photo to the camera film and return the logo
assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:[UIImage imageNamed:@"logo"]].placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (!success) {
NSLog(@" Save failed: %@", error);
return;
}
// Get a photo object according to the logo
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].lastObject;
// Get the custom photo album object
PHAssetCollection *collection = [self collection];
if (collection == nil) return;
// Save the photo to a custom album
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[[PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection] addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@" Save success ");
} else {
NSLog(@" Save failed: %@", error);
}
}];
}];
}
}
2. Search for all photos
/**
* Query all images
*/
- (IBAction)searchAllImages {
// Walk through all the custom albums
PHFetchResult<PHAssetCollection *> *collectionResult0 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in collectionResult0) {
[self searchAllImagesInCollection:collection];
}
// Get a picture of the camera film
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;
}
}
/**
* Look up all the pictures in an album
*/
- (void)searchAllImagesInCollection:(PHAssetCollection *)collection
{
// Take sync to get the picture (only get it 1 A picture)
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
imageOptions.synchronous = YES;
NSLog(@" Photo album name: %@", collection.localizedTitle);
// Walk through all the pictures in the album
PHFetchResult<PHAsset *> *assetResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
for (PHAsset *asset in assetResult) {
// Filter non-image
if (asset.mediaType != PHAssetMediaTypeImage) continue;
// Size of picture
CGSize targetSize = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
// Request the picture
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeDefault options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@" Image: %@", result);
}];
}
}
The above is the entire content of this article, I hope to help you with your study.