iOS Sample code for selecting multiple photos from a system album
- 2021-11-30 01:39:37
- OfStack
I have written UIImagePickerController to retrieve the system photo album pictures before (select a single photo, which is mostly used for the user's avatar)
1. pod import
pod 'CTAssetsPickerController'
2. Add a header file
#import <CTAssetsPickerController/CTAssetsPickerController.h>
3. Add delegate
<CTAssetsPickerControllerDelegate>
4. Implement
- (void)upLoading {
[PHPhotoLibraryrequestAuthorization:^(PHAuthorizationStatus status){
if (status !=PHAuthorizationStatusAuthorized)return;
dispatch_async(dispatch_get_main_queue(), ^{
CTAssetsPickerController *picker = [[CTAssetsPickerControlleralloc] init];
picker.delegate =self;
// Displays the selected index
picker.showsSelectionIndex =YES;
// Set the type of photo album: camera film + Custom album
picker.assetCollectionSubtypes =@[
@(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
@(PHAssetCollectionSubtypeAlbumRegular)];
// No need to display empty albums
picker.showsEmptyAlbums =NO;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
});
}];
}
-(BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
NSInteger max =9;
if (picker.selectedAssets.count >= max) {
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@" Prompt "message:[NSStringstringWithFormat:@" Maximum choice %zd A picture ", max] preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertActionactionWithTitle:@" OK "style:UIAlertActionStyleDefaulthandler:nil]];
[picker presentViewController:alertanimated:YEScompletion:nil];
// You can't use it here self Come modal Other controllers, because at this time self.view Not in window Upper
returnNO;
}
returnYES;
}
-(void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documents = [arraylastObject];
NSString *documentPath = [documentsstringByAppendingPathComponent:@"arrayXML.xml"];
NSArray *dataArray = [NSArrayarrayWithArray:assets];
[dataArray writeToFile:documentPathatomically:YES];
NSArray *resultArray = [NSArrayarrayWithContentsOfFile:documentPath];
NSLog(@"%@", documentPath);
// Close the picture selection interface
[picker dismissViewControllerAnimated:YEScompletion:nil];
// Traverse through all selected pictures
self.plCollection.photoArray = assets;
for (NSInteger i =0; i < assets.count; i++) {
// Basic configuration
CGFloat scale = [UIScreenmainScreen].scale;
PHImageRequestOptions *options = [[PHImageRequestOptionsalloc] init];
options.resizeMode =PHImageRequestOptionsResizeModeExact;
options.deliveryMode =PHImageRequestOptionsDeliveryModeHighQualityFormat;
PHAsset *asset = assets[i];
CGSize size =CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
// // Get a picture
[[PHImageManagerdefaultManager] requestImageForAsset:assettargetSize:size contentMode:PHImageContentModeDefaultoptions:options resultHandler:^(UIImage *_Nullable result,NSDictionary *_Nullable info) {
NSData *imageData =UIImageJPEGRepresentation([selfimageWithImageSimple:resultscaledToSize:CGSizeMake(200,200)], 0.5);
[selfossUpload:imageData];
}];
}
}