遇到这个需求是,自己也是一脸懵,以前获取过单张图。无奈之下只能尝试用系统的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];
}];
}
}];
}
网友评论