美文网首页
OC 选择图片,视频框架

OC 选择图片,视频框架

作者: Look2021 | 来源:发表于2022-05-25 17:56 被阅读0次

HXPhotoPicker

选取照片

WEAKSELF
HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
// 直接选择的是原图
manager.configuration.hideOriginalBtn = YES;
manager.configuration.requestImageAfterFinishingSelection = YES;
manager.configuration.requestOriginalImage = YES;
manager.configuration.openCamera = YES;
manager.configuration.photoMaxNum = 50;
[self hx_presentSelectPhotoControllerWithManager:manager didDone:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL isOriginal, UIViewController *viewController, HXPhotoManager *manager) {
    
    HXPhotoModel *hxModel = photoList.firstObject;
    [weakSelf doneBtnClickWithImage:hxModel.previewPhoto];
    
} cancel:^(UIViewController *viewController, HXPhotoManager *manager) {
    DEBUGLOG(@"block - 取消了");
}];
图片编辑
// 单独使用照片编辑功能
WEAKSELF
HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
[self hx_presentPhotoEditViewControllerWithManager:manager photoModel:baseModel.hxModel delegate:nil done:^(HXPhotoModel *beforeModel,
    HXPhotoModel *afterModel, HXPhotoEditViewController *viewController) {
    // beforeModel编辑之前、afterModel编辑之后
    
    weakSelf.imageArray[weakSelf.didImageSelect] = [[JMBaseImageModel alloc] initWithHXPhotoModel:afterModel];
    [weakSelf.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.didImageSelect inSection:0]]];
    
} cancel:^(HXPhotoEditViewController *viewController) {
    // 取消
}];
// 单独使用仿微信编辑功能
[self hx_presentWxPhotoEditViewControllerWithConfiguration:self.manager.configuration.photoEditConfigur photoModel:photoModel delegate:nil finish:^(HXPhotoEdit * _Nonnull photoEdit, HXPhotoModel * _Nonnull photoModel, HX_PhotoEditViewController * _Nonnull viewController) {
    if (photoEdit) {
        // 有编辑过
        weakSelf.imageView.image = photoEdit.editPreviewImage;
    }else {
        // 为空则未进行编辑
        weakSelf.imageView.image = photoModel.thumbPhoto;
    }
    // 记录下当前编辑的记录,再次编辑可在上一次基础上进行编辑
    weakSelf.photoEdit = photoEdit;
} cancel:^(HX_PhotoEditViewController * _Nonnull viewController) {
    // 取消
}];

选取视频

HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypeVideo];
manager.configuration.openCamera = NO;
manager.configuration.videoMaximumSelectDuration = 3 * 60 * 60;
manager.configuration.videoCanEdit = NO;
manager.configuration.videoMaxNum = 1;

kWEAKSELF
[self hx_presentSelectPhotoControllerWithManager:manager didDone:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL isOriginal, UIViewController *viewController, HXPhotoManager *manager) {
    
    DEBUGLOG(@"block - video - %@",videoList);
    HXPhotoModel *hxModel = videoList.firstObject;
    [hxModel requestAVAssetStartRequestICloud:nil progressHandler:nil success:^(AVAsset * _Nullable avAsset, AVAudioMix * _Nullable audioMix, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
        
        AVURLAsset * urlAsset = (AVURLAsset*)avAsset;
        // 导出完成, videoURL
        DEBUGLOG(@"路径 --  %@", urlAsset.URL.path);
        
        JPImageresizerConfigure *configure = [JPImageresizerConfigure defaultConfigureWithVideoAsset:urlAsset make:nil fixErrorBlock:nil fixStartBlock:nil fixProgressBlock:nil fixCompleteBlock:nil];
        DWVideoZiYouViewController *vc = [[DWVideoZiYouViewController alloc] init];
        vc.configure = configure;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    } failed:nil];
    
    
} cancel:^(UIViewController *viewController, HXPhotoManager *manager) {
    DEBUGLOG(@"block - 取消了");
}];

视频时长编辑,不是很好用

// 单独使用视频编辑功能
            HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
            manager.configuration.maxVideoClippingTime = 1 * 60 * 60;
            manager.configuration.videoMaximumSelectDuration = 1 * 60 * 60;
            manager.configuration.editVideoExportPreset = HXVideoEditorExportPresetMediumQuality;
            HXPhotoModel *videoModel = [HXPhotoModel photoModelWithVideoURL:urlAsset.URL];
            kWEAKSELF
            [self hx_presentVideoEditViewControllerWithManager:manager videoModel:videoModel delegate:nil done:^(HXPhotoModel *beforeModel,
                HXPhotoModel *afterModel, HXVideoEditViewController *viewController) {
                // beforeModel编辑之前、afterModel编辑之后
                DWSaveLocalVideoViewController *vc = [[DWSaveLocalVideoViewController alloc] init];
                vc.videoUrlString = afterModel.videoURL.path;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } cancel:^(HXVideoEditViewController *viewController) {
                // 取消
            }];

TZImagePickerController

选照片

TZImagePickerController *imagePC = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
imagePC.modalPresentationStyle = UIModalPresentationFullScreen;
// 是否可以选择视频
imagePC.allowPickingVideo = NO;
// 是否可以裁剪
imagePC.allowCrop = YES;
imagePC.scaleAspectFillCrop = YES;

kWEAKSELF
imagePC.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
    
    [TSWindowHudService showLoadingView];
    [UploadQiNiuService uploadImageArray:@[photos.firstObject] success:^(NSString * _Nonnull urlString) {
        
        [weakSelf User_UpdateFieldsApiWithDict:@{@"avatar" : urlString}];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
};
[self presentViewController:imagePC animated:YES completion:nil];

选视频,只能单选视频

// 视频提取
TZImagePickerController *imagePC = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
//            imagePC.barItemTextColor = k_Color_themeColor;
imagePC.allowPickingOriginalPhoto = NO;
//            imagePC.modalPresentationStyle = UIModalPresentationFullScreen;
// 是否可以选择视频
imagePC.allowPickingVideo = YES;
imagePC.allowPickingImage = NO;
imagePC.allowTakePicture = NO;
[self presentViewController:imagePC animated:YES completion:nil];
#pragma mark ------- TZImagePickerControllerDelegate -------

// 选择视频的回调
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset {
    PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.version = PHVideoRequestOptionsVersionOriginal;
    WEAKSELF
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        if ([asset isKindOfClass:[AVURLAsset class]]) {
            AVURLAsset * urlAsset = (AVURLAsset*)asset;
            NSNumber *size;
            [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
            DEBUGLOG(@"size is %f", [size floatValue] / (1024.0 * 1024.0));
            
        }
    }];
}
// 获取视频的Size
NSArray *array = self.urlAsset.tracks;
for (AVAssetTrack *track in array) {
    if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
        CGSize videoSize = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);
        videoSize = CGSizeMake(fabs(videoSize.width), fabs(videoSize.height));
        self.videoSize = videoSize;
//            DEBUGLOG(@"size %.0fx%.0f", self.videoSize.width, self.videoSize.height);
    }
}

相关文章

网友评论

      本文标题:OC 选择图片,视频框架

      本文链接:https://www.haomeiwen.com/subject/hdxyprtx.html