IOS - 照片/视频选择器

作者: 枫叶丶 | 来源:发表于2017-03-07 14:49 被阅读2770次

一个好用的照片选择器
github地址:https://github.com/LoveZYForever/HXWeiboPhotoPicker
照片选择器 支持 ios8 以上

IMG_4311.PNG
IMG_4308.PNG
IMG_4309.PNG

目录

特性 - Features

  • √ 查看/选择GIF图片
  • √ 照片、视频可同时多选/原图
  • √ 3DTouch预览照片
  • √ 长按拖动改变顺序
  • √ 自定义相机拍照/录制视频
  • √ 自定义转场动画
  • √ 查看/选择LivePhoto IOS9.1以上才有用
  • √ 支持浏览网络图片
  • √ 支持自定义裁剪图片
  • √ 观察系统相册变化实时增删
  • √ 支持传入本地图片
  • √ 支持在线下载iCloud上的资源

安装 - Installation

  • Cocoapods:pod 'HXWeiboPhotoPicker', '~> 2.1.5'搜索不到库或最新版请执行pod repo update
  • 手动导入:将项目中的“HXWeiboPhotoPicker”文件夹拖入项目中
  • 网络图片加载使用的是SDWebImage v4.0.0
  • 使用前导入头文件 "HXPhotoPicker.h"

要求 - Requirements

  • iOS8及以上系统可使用. ARC环境. - iOS 8 or later. Requires ARC
  • 在Xcode8环境下将项目运行在iOS10的设备/模拟器中,访问相册和相机需要配置三个info.plist文件
  • Privacy - Photo Library Usage Description 和 Privacy - Camera Usage Description 以及 Privacy - Microphone Usage Description
  • 相机拍照功能请使用真机调试

应用示例 - Examples

Demo1

// 懒加载 照片管理类
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    }
    return _manager;
}

// 一个方法调用
__weak typeof(self) weakSelf = self;
[self hx_presentAlbumListViewControllerWithManager:self.manager done:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL original, HXAlbumListViewController *viewController) {
    weakSelf.total.text = [NSString stringWithFormat:@"总数量:%ld   ( 照片:%ld   视频:%ld )",allList.count, photoList.count, videoList.count];
    weakSelf.original.text = original ? @"YES" : @"NO";
    NSSLog(@"all - %@",allList);
    NSSLog(@"photo - %@",photoList);
    NSSLog(@"video - %@",videoList);
} cancel:^(HXAlbumListViewController *viewController) {
    NSSLog(@"取消了");
}];

// 照片选择控制器
HXAlbumListViewController *vc = [[HXAlbumListViewController alloc] init];
vc.delegate = self;
vc.manager = self.manager; 
[self presentViewController:[[HXCustomNavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];

// 通过 HXPhotoViewControllerDelegate 代理返回选择的图片以及视频
- (void)albumListViewController:(HXAlbumListViewController *)albumListViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original

// 点击取消
- (void)albumListViewControllerDidCancel:(HXAlbumListViewController *)albumListViewController

Demo2

// 懒加载 照片管理类
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    }
    return _manager;
}

self.navigationController.navigationBar.translucent = NO;
self.automaticallyAdjustsScrollViewInsets = YES;

HXPhotoView *photoView = [[HXPhotoView alloc] initWithFrame:CGRectMake((414 - 375) / 2, 100, 375, 400) manager:self.manager];
photoView.delegate = self;
photoView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:photoView];

// 代理返回 选择、移动顺序、删除之后的图片以及视频
- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal;

// 当view更新高度时调用
- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame;

// 删除网络图片的地址
- (void)photoView:(HXPhotoView *)photoView deleteNetworkPhoto:(NSString *)networkPhotoUrl;

Demo3

- (HXPhotoManager *)manager { // 懒加载管理类
    if (!_manager) { // 设置一些配置信息
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        //        _manager.openCamera = NO;
        _manager.showDeleteNetworkPhotoAlert = NO;
        _manager.saveSystemAblum = YES;
        _manager.photoMaxNum = 6;
        _manager.maxNum = 6;
        // 可以这个赋值也可以像下面那样
//       _manager.networkPhotoUrls = [NSMutableArray arrayWithObjects:@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/003d86db-b140-4162-aafa-d38056742181.jpg",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/0034821a-6815-4d64-b0f2-09103d62630d.jpg",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/0be5118d-f550-403e-8e5c-6d0badb53648.jpg",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/1466408576222.jpg", nil];
    }
    return _manager;
}
CGFloat width = scrollView.frame.size.width;
HXPhotoView *photoView = [HXPhotoView photoManager:self.manager];
photoView.frame = CGRectMake(kPhotoViewMargin, kPhotoViewMargin, width - kPhotoViewMargin * 2, 0);
photoView.delegate = self;
photoView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:photoView];
self.photoView = photoView;
    
 // 可以在懒加载中赋值 ,  也可以这样赋值
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"http://oss-cn-hangzhou.aliyuncs.com/tsnrhapp/shop/photos/857980fd0acd3caf9e258e42788e38f5_0.gif",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/0034821a-6815-4d64-b0f2-09103d62630d.jpg",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/0be5118d-f550-403e-8e5c-6d0badb53648.jpg",@"http://tsnrhapp.oss-cn-hangzhou.aliyuncs.com/1466408576222.jpg", nil];
[self.manager addNetworkingImageToAlbum:array selected:YES];
// 设置完网络图片地址数组后重新刷新视图
[self.photoView refreshView];

Demo4

- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.openCamera = YES;
        // 在这里设置为单选模式
        _manager.singleSelected = YES;
        // 单选模式下选择图片时是否直接跳转到编辑界面
        _manager.configuration.singleJumpEdit = YES;
        // 是否可移动的裁剪框
        _manager.configuration.movableCropBox = YES;
        // 可移动的裁剪框是否可以编辑大小
        _manager.configuration.movableCropBoxEditSize = YES;
    }
    return _manager;
}
__weak typeof(self) weakSelf = self;
[self hx_presentAlbumListViewControllerWithManager:self.manager done:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL original, HXAlbumListViewController *viewController) {
    if (photoList.count > 0) {
//            HXPhotoModel *model = photoList.firstObject;
//            weakSelf.imageView.image = model.previewPhoto;
        [weakSelf.view showLoadingHUDText:@"获取图片中"];
        [weakSelf.toolManager getSelectedImageList:photoList requestType:0 success:^(NSArray<UIImage *> *imageList) {
            [weakSelf.view handleLoading];
            weakSelf.imageView.image = imageList.firstObject;
        } failed:^{
            [weakSelf.view handleLoading];
            [weakSelf.view showImageHUDText:@"获取失败"];
        }];
        NSSLog(@"%ld张图片",photoList.count);
    }else if (videoList.count > 0) {
        [weakSelf.toolManager getSelectedImageList:allList success:^(NSArray<UIImage *> *imageList) {
            weakSelf.imageView.image = imageList.firstObject;
        } failed:^{

        }];

        // 通个这个方法将视频压缩写入临时目录获取视频URL  或者 通过这个获取视频在手机里的原路径 model.fileURL  可自己压缩
        [weakSelf.view showLoadingHUDText:@"视频写入中"];
        [weakSelf.toolManager writeSelectModelListToTempPathWithList:videoList success:^(NSArray<NSURL *> *allURL, NSArray<NSURL *> *photoURL, NSArray<NSURL *> *videoURL) {
            NSSLog(@"%@",videoURL);
            [weakSelf.view handleLoading];
        } failed:^{
            [weakSelf.view handleLoading];
            [weakSelf.view showImageHUDText:@"写入失败"];
            NSSLog(@"写入失败");
        }];
        NSSLog(@"%ld个视频",videoList.count);
    }
} cancel:^(HXAlbumListViewController *viewController) {
    NSSLog(@"取消了");
}];

Demo5

// 懒加载三个管理类用来控制三个选择器
- (HXPhotoManager *)oneManager {
    if (!_oneManager) {
        _oneManager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
    }
    return _oneManager;
}
- (HXPhotoManager *)twoManager {
    if (!_twoManager) {
        _twoManager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypeVideo];
    }
    return _twoManager;
}
- (HXPhotoManager *)threeManager {
    if (!_threeManager) {
        _threeManager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    }
    return _threeManager;
}
// 初始化UIScrollerView以及三个HXPhotoView
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.scrollView.alwaysBounceVertical = YES;
[self.view addSubview:self.scrollView];
    
self.onePhotoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(kPhotoViewMargin, kPhotoViewMargin, self.view.frame.size.width - kPhotoViewMargin * 2, 0) WithManager:self.oneManager];
self.onePhotoView.delegate = self;
[self.scrollView addSubview:self.onePhotoView];
    
self.twoPhotoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.onePhotoView.frame) + kPhotoViewSectionMargin, self.view.frame.size.width - kPhotoViewMargin * 2, 0) WithManager:self.twoManager];
self.twoPhotoView.delegate = self;
[self.scrollView addSubview:self.twoPhotoView];
    
self.threePhotoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.twoPhotoView.frame) + kPhotoViewSectionMargin, self.view.frame.size.width - kPhotoViewMargin * 2, 0) WithManager:self.threeManager];
self.threePhotoView.delegate = self;
[self.scrollView addSubview:self.threePhotoView];

// 根据photoView来判断是哪一个选择器
- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    if (self.onePhotoView == photoView) {
        NSSLog(@"onePhotoView - %@",allList);
    }else if (self.twoPhotoView == photoView) {
        NSSLog(@"twoPhotoView - %@",allList);
    }else if (self.threePhotoView == photoView) {
        NSSLog(@"threePhotoView - %@",allList);
    }
}
// 返回更新后的frame,根据photoView来更新scrollView
- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame {
    if (self.onePhotoView == photoView) {
        self.twoPhotoView.frame = CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.onePhotoView.frame) + kPhotoViewSectionMargin,                 self.view.frame.size.width - kPhotoViewMargin * 2, self.twoPhotoView.frame.size.height);
        self.threePhotoView.frame = CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.twoPhotoView.frame) + kPhotoViewSectionMargin,               self.view.frame.size.width - kPhotoViewMargin * 2, self.threePhotoView.frame.size.height);
    }else if (self.twoPhotoView == photoView) {
        self.twoPhotoView.frame = CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.onePhotoView.frame) + kPhotoViewSectionMargin,                 self.view.frame.size.width - kPhotoViewMargin * 2, self.twoPhotoView.frame.size.height);
        self.threePhotoView.frame = CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.twoPhotoView.frame) + kPhotoViewSectionMargin,               self.view.frame.size.width - kPhotoViewMargin * 2, self.threePhotoView.frame.size.height);
    }else if (self.threePhotoView == photoView) {
        self.threePhotoView.frame = CGRectMake(kPhotoViewMargin, CGRectGetMaxY(self.twoPhotoView.frame) + kPhotoViewSectionMargin,               self.view.frame.size.width - kPhotoViewMargin * 2, frame.size.height);
    }
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.threePhotoView.frame) + kPhotoViewMargin);
}

Demo6

// 先在第一个控制器里初始化管理类并设置好属性
- (HXPhotoManager *)manager {
    if (!_manager) {
        /**  注意!!! 如果是先选照片拍摄的话, 不支持将拍摄的照片或者视频保存到系统相册  **/
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
        _manager.outerCamera = YES;
        _manager.openCamera = NO;
        _manager.saveSystemAblum = YES;
    }
    return _manager;
}
// 通过HXPhotoViewController的代理跳转界面并将当前界面的管理类传入下一个界面
- (void)photoViewControllerDidNext:(NSArray<HXPhotoModel *> *)allList Photos:(NSArray<HXPhotoModel *> *)photos Videos:(NSArray<HXPhotoModel *> *)videos Original:(BOOL)original {
    Demo6SubViewController *vc = [[Demo6SubViewController alloc] init];
    vc.manager = self.manager;
    [self.navigationController pushViewController:vc animated:YES];
}
// 这里需要注意在第二个控制释放的时候需要将已选的操作清空
- (void)dealloc { 
    [self.manager clearSelectedList];
}

Demo7

// 加载本地图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0 ; i < 4; i++) {
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
    [images addObject:image];
}
    
CGFloat width = scrollView.frame.size.width;
HXPhotoView *photoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(kPhotoViewMargin, kPhotoViewMargin, width - kPhotoViewMargin * 2, 0) manager:self.manager];
photoView.delegate = self;
photoView.backgroundColor = [UIColor whiteColor];
// 在这里将本地图片image数组给管理类并且刷新界面
[self.manager addLocalImage:images selected:YES];
[photoView refreshView];
[scrollView addSubview:photoView];
self.photoView = photoView;

Demo8

- (HXDatePhotoToolManager *)toolManager {
    if (!_toolManager) {
        _toolManager = [[HXDatePhotoToolManager alloc] init];
    }
    return _toolManager;
}
[self.view showLoadingHUDText:@"写入中"];
__weak typeof(self) weakSelf = self;
HXDatePhotoToolManagerRequestType requestType;
if (self.original) {
    requestType = HXDatePhotoToolManagerRequestTypeOriginal;
}else {
    requestType = HXDatePhotoToolManagerRequestTypeHD;
}
[self.toolManager writeSelectModelListToTempPathWithList:self.selectList requestType:requestType success:^(NSArray<NSURL *> *allURL, NSArray<NSURL *> *photoURL, NSArray<NSURL *> *videoURL) {
    NSSLog(@"\nall : %@ \nimage : %@ \nvideo : %@",allURL,photoURL,videoURL);
    NSURL *url = photoURL.firstObject;
    if (url) {
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        NSSLog(@"%@",image);
    }
            [weakSelf.view handleLoading];
} failed:^{
    [weakSelf.view handleLoading];
    [weakSelf.view showImageHUDText:@"写入失败"];
    NSSLog(@"写入失败");
}];

Demo9

具体代码请下载工程查看demo9

相关文章

网友评论

  • 弯弯的小溪_1108:<HXPhotoModel:0x18a98070>图片信息怎样用PHAsset对象获取呢,能给下详细代码吗
  • dc0e5eac9b64:楼主,这个编辑照片时的裁剪框怎么去掉啊,找了好久没成功
    dc0e5eac9b64:@枫叶丶 想只能旋转,不改变大小
    枫叶丶:@HHello_Hello 不要裁剪框怎么编辑呀。。
  • dc0e5eac9b64:demo5 里面怎么上传图片啊?NSArray photo = self.oneManager.afterSelectedPhotoArray得到的<HXPhotoModel:0x18a98070>类型,不知道怎么上传
    弯弯的小溪_1108:<HXPhotoModel:0x18a98070>图片信息怎样用PHAsset对象获取呢,能给下详细代码吗
    dc0e5eac9b64:@枫叶丶 谢谢!我找到了。还有一个问题,就是没法显示https的网络图片,图片地址没错,在浏览器上可以查看
    枫叶丶:可以用模型里面PHAsset对象获取图片信息
  • 遇见Miu:楼主,Demo3的网络获取的gif不会动,请问怎么解决?
    枫叶丶:@萧奇 最新版已支持网络gif图片
  • ded3373920e6:长图编辑旋转会闪退
    枫叶丶:@th997102525 👌🏻
    ded3373920e6:@枫叶丶 不是长图是全景图,编辑状态 。旋转会退
    枫叶丶:@th997102525 刚试了长图没问题呀
  • 3fdb581ec682:图片和视频怎么同时选择求解释
    3fdb581ec682:@枫叶 问下大神怎么可以去掉图片选择时候的右上角的下载按钮,或者隐藏掉也行,在选择的时候直接提醒不可选择
    3fdb581ec682:@枫叶丶 大神您的qq多少了,我想问下有个效果能不能实现我的qq147981207,加下我可以聊下
    枫叶丶:@南海一页扁舟
    /**
    图片和视频是否能够同时选择 默认支持
    */
    @property (assign, nonatomic) BOOL selectTogether;
  • 幻想无极:看起来来不错
  • 82d4ee28ea19:demo9中,在cell中加入_photoView.outerCamera = YES;然后直接使用相机拍照,cell不会展示出图片,只有先在相册中选择以后,图片才能通过拍照加入cell中,求解决!谢谢!
  • 305365d2b980:上传完照片以后下次进到页面怎么预览网络照片?
  • Davis_:笔者QQ1711647013,针对刚刚提出的问题,希望加我讨论一下
    枫叶丶:@Davis_ 好的,我不怎么上简书。现在才看到
  • Davis_:当使用icloud之后,选择gif图片时,gif不会播放,有可能是指获取到了一张图片
  • koreadragon:选择完照片,通过HXPhotoTools取出来的照片,咋这么小呢,怎么取到原图?谢谢
    枫叶丶:@koreadragon 最下面
    koreadragon:@枫叶丶 GitHub上只写到demo7了啊,没有demo8
    枫叶丶:最新版,看demo8
  • Sum123:你好 使用2.1.4版本 swift cocoapods会报错 把#import "UIImageView+WebCache.h"放到.m文件里就好了
    还有 能不能把默认原图的接口放出来 并且原图对比iOS相册里也不是很清晰 
    多谢轮子 希望更好用
    枫叶丶:恩,后面会加一个type区别原图和高清图
  • 6cacec483f07:您好,我通过 [HXPhotoTools getImageForSelectedPhoto:photos type:0 completion:^(NSArray<UIImage *> *images) {}方法获取的图片 调用UIImageJPEGRepresentation方法转成NSData的时候部分图片会返回nil,请问您知道可能是什么原因吗
  • 9f7b948d1c8e:请问,在我选择照片上传时,如遇到上传失败,怎么把图片清除
    枫叶丶:@加加加_ 有个方法可以清除,demo里面有... 我不怎么看简书..可能回答慢点
    9f7b948d1c8e:现在选完照片后,默认已是显示了,或者能不能在我上传成功之后再把图片显示出来,上传失败这个不知道怎么处理,希望得到回复
  • 许久__:在哪个方法里面将选择到的图片上传至服务器??
  • 幸运儿_2596:博主你好,请问一下怎么获取图片进行上传啊?不能使用HXPhotoModel的小图,看了一下tools里面的方法,全部都是必须先预览图片才能拿到原始图片,求解答 。。。。。急@枫叶 、
    幸运儿_2596:@枫叶丶 谢谢!
    枫叶丶:Demo8里有方法 返回的是URL
  • 青色石头_cy:现在的版本,跟微信朋友圈一样?返回到相册列表,不是顶部展开相册列表了?
    不知蜕变的挣扎:有找到顶部展开相册列表的库吗?
  • 孜孜_:楼主,如果用你的框架 上传多张图片,应该 用哪个属性值传给后台
    枫叶丶:HXDatePhotoToolManager 这个类有方法将模型数组里的资源写入临时文件返回NSURL 对象可以拿这个url上传
  • 飞奔的羊:为什么选择完照片出来以后会有一下小卡顿...:scream:
    枫叶丶:@青蛙兔子 你可以在将配置类里的cameraCellShowPreview 这个属性设置成NO在列表界面不预览相机,目前还不知道为什么引起会这样..
    1ce1b150f42c:确实,5S上尤其明显,卡住好几秒不动,楼主能帮忙解答下吗?
  • 兰德耍:楼主 这个我用在iOS 11 上面 选择图片的节目 导航栏往上移动了20 有解决办法吗 :smiley:
  • 兰德耍:楼主。我如何修改选择照片后 显示这个照片的大小呢 你这个默认是每行显示三个 我想修改HXPhotoView 这个view里面每个照片item的大小。比如我想一行 显示四个 或者个 5个的呢。谢谢🙏
    d19606e92aa5:@枫叶丶 你好 是哪个属性
    d19606e92aa5:你好,这个问题你解决了吗 我qq674114220 如果解决了告诉我一声 谢谢
    枫叶丶:新版属性已暴露
  • ZzQzz:好像demo里面没有发文字的那部分哦
    枫叶丶:确实没有:smile: 那是之前用到的项目顺便截个图
  • 9ad14089cbfb:项目中其他位置用的sdwebImg ,兼容么????
    枫叶丶:@9ad14089cbfb sdwebimg 用到是v4.0.0 版本

本文标题:IOS - 照片/视频选择器

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