【iOS】 图片,视频那些事

最近找了个三方的图片选择器QBImagePickerController,集成到项目中,基本功能都满足了,那么就它了,顺便写了点其他应用相册选取图片视频的代码,下面记录下来


【iOS】 图片,视频那些事_第1张图片
Snip20170215_1.png

前四个都是采用系统的实现的,多图选择使用的是三方QBImagePickerController,先从QBImagePickerController的使用说起;
导入头文件

#import 

初始化QBImagePickerController

//选择多张图
- (void)loadQBImagePickerController
{
    QBImagePickerController *QBImagePickerC = [[QBImagePickerController alloc]init];
    QBImagePickerC.delegate = self;
    QBImagePickerC.allowsMultipleSelection = YES;//是否允许多选
    QBImagePickerC.maximumNumberOfSelection = 9;//最多可选数量
    QBImagePickerC.mediaType = QBImagePickerMediaTypeImage;//三种类型 图片,视频,图片和视频
    QBImagePickerC.numberOfColumnsInPortrait = 4;//竖屏下一行现实的照片张数
    QBImagePickerC.numberOfColumnsInLandscape = 7;//横屏下一行现实的照片张数
    QBImagePickerC.prompt = @"请选择照片";//界面上方现实的文字
    QBImagePickerC.showsNumberOfSelectedAssets = YES;//是否显示已选数量
    QBImagePickerC.assetCollectionSubtypes = @[@(PHAssetCollectionSubtypeSmartAlbumUserLibrary), //相机胶卷
        @(PHAssetCollectionSubtypeAlbumMyPhotoStream), //我的照片流
        @(PHAssetCollectionSubtypeSmartAlbumPanoramas), //全景图
        @(PHAssetCollectionSubtypeSmartAlbumVideos), //视频
        @(PHAssetCollectionSubtypeSmartAlbumBursts) //连拍模式拍摄的照片
                                               ];
    [self presentViewController:QBImagePickerC animated:YES completion:nil];
}

QBImagePickerController的代理方法

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets;
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController;
- (BOOL)qb_imagePickerController:(QBImagePickerController *)imagePickerController shouldSelectAsset:(PHAsset *)asset;
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(PHAsset *)asset;
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didDeselectAsset:(PHAsset *)asset;

前两个代理方法最为常用:
第一个是选中图片后点击done的回调;
第二个是取消的回调;

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets
{
    [self.imageArr removeAllObjects];
    for (PHAsset *set in assets) {
        if(set.mediaType == PHAssetMediaTypeImage){
            PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
            options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
            [[PHImageManager defaultManager] requestImageForAsset:set targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
                //设置图片
                [self.imageArr addObject:result];
            }];
        }
    }
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

在done的回调中获取图片,获取图片后去进行需要的操作,例如压缩,上传等,这里我就不过多介绍了;
接下来是用系统方法获取图片和视频的代码;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIAlertController *action = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [action addAction:[UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadCameraMovie];
    }]];
    [action addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadCamera];
    }]];
    [action addAction:[UIAlertAction actionWithTitle:@"从相册选择视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadPhotoLibraryMovie];
    }]];
    [action addAction:[UIAlertAction actionWithTitle:@"从相册选择照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadPhotoLibraryPhoto];
    }]];
    [action addAction:[UIAlertAction actionWithTitle:@"从相册选择多张照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadQBImagePickerController];
    }]];
    
    [action addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:action animated:YES completion:nil];
}

拍视频

//拍摄
- (void)loadCameraMovie
{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"对不起,您的设备不支持拍摄功能" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        UIImagePickerController *pic = [[UIImagePickerController alloc]init];
        pic.sourceType = UIImagePickerControllerSourceTypeCamera;
        pic.allowsEditing = YES;
        pic.delegate = self;
        pic.mediaTypes = @[(NSString *)kUTTypeMovie];
        pic.videoMaximumDuration = 10;
        [self presentViewController:pic animated:YES completion:nil];
    }
}

拍照片

//拍照
- (void)loadCamera
{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"对不起,您的设备不支持拍照功能" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        UIImagePickerController *pic = [[UIImagePickerController alloc]init];
        pic.sourceType = UIImagePickerControllerSourceTypeCamera;
        pic.mediaTypes = @[(NSString *)kUTTypeImage];
        pic.delegate = self;
        pic.allowsEditing = YES;
        [self presentViewController:pic animated:YES completion:nil];
    }
}

从相册选视频

//从相册选视频
- (void)loadPhotoLibraryMovie
{
    UIImagePickerController *pic = [[UIImagePickerController alloc]init];
    pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pic.mediaTypes = @[(NSString *)kUTTypeMovie];
    pic.delegate = self;
    pic.allowsEditing = YES;
    [self presentViewController:pic animated:YES completion:nil];
}

从相册选照片

//从相册选照片
- (void)loadPhotoLibraryPhoto
{
    UIImagePickerController *pic = [[UIImagePickerController alloc]init];
    pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pic.mediaTypes = @[(NSString *)kUTTypeImage];
    pic.delegate = self;
    pic.allowsEditing = YES;
    [self presentViewController:pic animated:YES completion:nil];
}

系统的回调

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if([info[@"UIImagePickerControllerMediaType"]isEqualToString:(NSString *)kUTTypeImage]){
        UIImage *pickerImage = info[@"UIImagePickerControllerOriginalImage"];
        self.headimage.image = pickerImage;
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    if([info[@"UIImagePickerControllerMediaType"]isEqualToString:(NSString *)kUTTypeMovie]){
        NSURL *movieUrl = info[@"UIImagePickerControllerMediaURL"];
        self.headimage.image = [self getVideoPreViewImage:movieUrl];
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

获取视频的缩略图

- (UIImage *)getVideoPreViewImage:(NSURL *)videoPath
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoPath options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 10);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *img = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return img;
}

你可能感兴趣的:(【iOS】 图片,视频那些事)