利用第三方CTAssetsPickerController实现此功能
引入头部
#import <CTAssetsPickerController.h>
设置代理
@interface MYNewViewController () <CTAssetsPickerControllerDelegate>
viewDidLoad方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
[self setNavBar];
UIImageView *image = [[UIImageView alloc] init];
self.imageView = image;
[image sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531358828&di=80359a01046f117d18a6c4b49507f413&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01f142580daa7ea84a0d304f48cb49.jpg%401280w_1l_2o_100sh.jpg"]];
image.MY_Width = 100;
image.MY_Height = 100;
image.MY_center = self.view.center;
[self.view addSubview:image];
//添加Lable
UILabel *txtLabe = [[UILabel alloc] init];
txtLabe.userInteractionEnabled = YES;
[txtLabe addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleImagePicker)]];
txtLabe.frame = CGRectMake(image.MY_x, image.MY_y+100, 100, 100);
txtLabe.text = @"多选图片";
[self.view addSubview:txtLabe];
}
手势触发方法
- (void) doubleImagePicker {
//请求/检查权限
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
//这个方法是在子线程执行 我们需要他回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// init Picker
CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
//指定获取哪个胶卷
picker.assetCollectionSubtypes = @[@(PHAssetCollectionSubtypeSmartAlbumUserLibrary), @(PHAssetCollectionSubtypeAlbumRegular)];
//set delegate
picker.delegate = self;
// to present picker as a from sheet in ipad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
picker.modalPresentationStyle = UIModalPresentationFormSheet;
}
//present picker
[self presentViewController:picker animated:YES
completion:nil];
});
}];
}
代理方法
- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
//关闭图片选中界面
[picker dismissViewControllerAnimated:YES completion:nil];
PHImageRequestOptions *options= [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
//显示图片
//注意点:NSFastEnumeration 只要类遵守这个代理就能使用for in 循环
// for (PHAsset *asset in assets) {
// NSLog(@"%@",asset);
// }
for (NSInteger i = 0 ; i<assets.count; i++) {
PHAsset *asset = assets[i];
//取出屏幕缩放比
CGFloat scale = UIScreen.mainScreen.scale;
//图片本身大小
CGSize size = CGSizeMake(asset.pixelWidth/scale, asset.pixelHeight/scale);
//请求图片
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@"%@",result);
//添加图片控件
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image =result;
[self.view addSubview:imageView];
//九宫格布局
imageView.frame = CGRectMake((i%3)*(100+10),(i/3)*(100+10), 100, 100);
}];
}
}