iOS 相簿選中多張圖片

weixin_33907511發表於2017-11-09

遇到這個需求是,自己也是一臉懵,以前獲取過單張圖。無奈之下只能嘗試用系統的UIImagePickerController實現,結果,可能是個人能力太渣,只能實現選中一張,在跳進去,在選中一張,如此迴圈下去。雖然也實現了功能,但是看這它一閃一閃的,著實鬧心。最終,決定,能用第三方,就別白白浪費著腦細胞。

首先 cocaPods整合
pod search CTAssetsPickerController
pod install

如果大神想手動整合
傳送門在此祝你好運

<CTAssetsPickerControllerDelegate> //代理協議

- (void)lyacquiring{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){ if (status !=PHAuthorizationStatusAuthorized)return; dispatch_async(dispatch_get_main_queue(), ^{
CTAssetsPickerController *assetsPicker = [[CTAssetsPickerController alloc] init];
assetsPicker.delegate =self;
// 顯示選擇的索引 assetsPicker.showsSelectionIndex =YES;
// 設定相簿的型別 assetsPicker.assetCollectionSubtypes =@[@(PHAssetCollectionSubtypeSmartAlbumUserLibrary),@(PHAssetCollectionSubtypeAlbumRegular)];
picker.showsEmptyAlbums =NO;
[self presentViewController: assetsPicker animated:YES completion:nil];
});
}];
}

-(BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset {
//設定想要選擇多少張
NSInteger max =4;
if (picker.selectedAssets.count >= max) {
return NO;
}
return YES;
}

- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray<PHAsset *> *)assets {
[picker dismissViewControllerAnimated:YES completion:^{
CGFloat lysize = [UIScreen mainScreen].scale;
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init]; requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
for (NSInteger i = 0; i < assets.count; i++) {
PHAsset *asset = assets[i];
CGSize size = CGSizeMake(asset.pixelWidth / lysize, asset.pixelHeight / lysize);
// 獲取圖片
/*這個鱉孫可給我坑苦了,切記真機下使用,要不坑哭你,在模擬器上獲取不到對應的圖片路徑,害得我好半天才搞明白,還有就是圖片不存在相簿也不行,好比說icloud中也獲取不到*/
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:requestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
[self.imageArr addObject:result];
}];
}
}];
}

相關文章