iOS - 使用 QBImagePickerController 从系统相册选取多张照片

梁丘兴腾
2023-12-01
  • 使用pod引入
pod 'QBImagePickerController', '~>2.6.0' 
  • 导入
#import <QBImagePickerController.h>
  • 加入代理
@interface ImagePickeriewController ()<QBImagePickerControllerDelegate>

@end
  • init
QBImagePickerController *picker = [[QBImagePickerController alloc] init];
picker.maximumNumberOfSelection = 5;
picker.prompt = @"选取图片";
picker.allowsMultipleSelection = YES;
picker.showsNumberOfSelectedAssets = YES;
picker.delegate = self;
picker.automaticallyAdjustsScrollViewInsets = NO;
[self presentViewController:picker animated:YES completion:nil];
  • delegate方法
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    NSMutableArray *imageMutableArray = [[NSMutableArray alloc] init];
    [imageMutableArray removeAllObjects];
    NSInteger n = 0;
    for (ALAsset * asset in assets) {
        CGImageRef ref = [asset thumbnail];    //获取缩略图
        UIImage *thumbnailImg = [[UIImage alloc]initWithCGImage:ref];
        [imageMutableArray addObject:thumbnailImg];
        n++;
    }
    
    UIImageView *imageView;
    for (int i = 0; i < n; i++) {
        imageView = [[UIImageView alloc] init];
        imageView.image = imageMutableArray[i];
        [self.view addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.photoImage.mas_left).offset(45 * i);
            make.bottom.equalTo(self.photoView.mas_bottom).offset(-10);
            make.size.mas_equalTo(CGSizeMake(35, 35));
        }];
    }

    _photoImage = [[UIImageView alloc] init];
    _photoImage.image = [UIImage imageNamed:@"picture.png"];
    [self.view addSubview:self.photoImage];
    [self.photoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.centerY.equalTo(imageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];

    _photoImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *photoImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoImageClicked)];
    photoImageTap.numberOfTapsRequired = 1;
    photoImageTap.numberOfTouchesRequired = 1;
    [_photoImage addGestureRecognizer:photoImageTap];

}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
    [self dismissViewControllerAnimated:YES completion:nil];
}

附:
获得的ALAsset对象就是相片对象:其中有相片的缩略图,全屏图,高清图,url等属性。
ALAsset *result = [assets objectAtIndex:index];

获取url:
String类型:
NSString *url = [[[result
defaultRepresentation]url]description];

URL类型:
NSURL *url = [[result defaultRepresentation]url];

获取缩略图:
CGImageRef ref = [result thumbnail];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取全屏相片:
CGImageRef ref = [[result defaultRepresentation]fullScreenImage];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取高清相片:
CGImageRef ref = [[result defaultRepresentation]fullResolutionImage];
UIImage *img = [[UIImage alloc]initWithCGImage:ref];

参考:http://blog.csdn.net/CHN_liu/article/details/45599511

 类似资料: