QBImagePicker用法详解

QBImagePicker是一款用于iOS平台的图像选择组件,支持从相册选取图片和视频,具备多选功能。该组件可通过CocoaPods集成或手动导入项目,使用时需遵循QBImagePickerControllerDelegate协议。它提供了丰富的配置选项,如限制选择数量、指定显示相册等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、简要说明

  • QBImagePicker用于获取iOS本地图像,QBImagePickerController扩展了UIImagePickerController类用于支持图像的多选操作。

QBImagePicker下载地址

2、集成方式

一、通过Cocoapods

  • 将 pod "QBImagePickerController" 添加到Podfile文件中,然后在终端执行命令pod install
  • 在要使用的类中引入头文件

#import <QBImagePickerController/QBImagePickerController.h>

  • 遵守协议QBImagePickerControllerDelegate

@interface ViewController()<QBImagePickerControllerDelegate>

  • 创建QBImagePickerController的实例并设置代理

QBImagePickerController *imagePickerController =[QBImagePickerController new];
imagePickerController.delegate = self;

二、手动

  • 拖拽 QBImagePicker文件夹到你的项目
  • 在要使用的类中引入头文件

#import "QBImagePickerController.h"

  • 遵守协议QBImagePickerControllerDelegate

@interface ViewController()<QBImagePickerControllerDelegate>

  • 创建QBImagePickerController的实例并设置代理

QBImagePickerController *imagePickerController =[QBImagePickerController new];
imagePickerController.delegate = self;

3、主要用法及功能

QBImagePicker用于获取iOS本地图像,并支持获取多个图片或视频。

QBImagePickerController *imagePickerController = [QBImagePickerController new];
imagePickerController.delegate = self;
imagePickerController.allowsMultipleSelection = YES;
[self presentViewController:imagePickerController animated:YES completion:nil];

1.获取到图像

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
    for (PHAsset *asset in assets) {
        // Do something with the asset
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

2.取消获取图像

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

3.更改选择图像时获取到通知

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didDeselectAsset:(PHAsset *)asset{
    NSLog(@"取消选中");
}
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(PHAsset *)asset{
    NSLog(@"选中");
}
- (BOOL)qb_imagePickerController:(QBImagePickerController *)imagePickerController shouldSelectAsset:(PHAsset *)asset{
    return YES;
}

4.设置选择图像数量

imagePickerController.allowsMultipleSelection = YES;//允许多选(默认是NO,不允许多选)
imagePickerController.minimumNumberOfSelection = 3;//设置选择图像数量下限
imagePickerController.maximumNumberOfSelection = 6;//设置选择图像数量上限

5.指定要显示的相册

imagePickerController.assetCollectionSubtypes = @[
    @(PHAssetCollectionSubtypeSmartAlbumUserLibrary), //相机胶卷
    @(PHAssetCollectionSubtypeAlbumMyPhotoStream), //我的照片流 
    @(PHAssetCollectionSubtypeSmartAlbumPanoramas), //全景图
    @(PHAssetCollectionSubtypeSmartAlbumVideos), //视频
    @(PHAssetCollectionSubtypeSmartAlbumBursts) //连拍模式拍摄的照片
];

6.设置媒体类型

imagePickerController.mediaType = QBImagePickerMediaTypeAny;//图片和视频
imagePickerController.mediaType = QBImagePickerMediaTypeImage;//图片
imagePickerController.mediaType = QBImagePickerMediaTypeVideo;//视频

7.设置每行显示的图像数量

imagePickerController.numberOfColumnsInPortrait = 4;//竖屏模式下一行显示4张图像
imagePickerController.numberOfColumnsInLandscape = 7;//横屏模式下一行显示7张图像

8.显示提示信息

imagePickerController.prompt = @"请选择图像!";//在界面上方显示文字“请选择图像!”
imagePickerController.showsNumberOfSelectedAssets = NO;//在界面下方显示已经选择图像的数量

 4、当前版本

3.4.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值