简单集成高大上的ImagePicker无标题文章
简单集成高大上的ImagePicker无标题文章
现在是个项目就要有图片上传所以下面介绍一下简单高端的图片选择器
感谢读者的细心发现bug,最近bug已经修复(github更新)所以对文章部分内容做出修改。
然后看一下实现代码 和效果图对比你就知道绝对可以带你装b带你飞!

B4F25DEC157D88BE0026E2246BB3445A.png

7D1EE10E2E807ED40427A9637211424B.jpg

8C2FC9D402191A7AD594F40FBA5AF23B.png

09D89C93E76EB1D38884A776AE486A59.png

AE2B983D7094C46948C372FD191D90BB.jpg
实现代码 可选择代理和block 我就直接都写上了 自己选择
#import"DzyImgPicker.h"#define
DzyWid ([UIScreen mainScreen].bounds.size.width)#define DzyHei
([UIScreen mainScreen].bounds.size.height)@interfaceViewController() {
DzyImgPicker *DzyView;}@end- (void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];//从相册返回的话需要刷新下界面[DzyView.collectionView
reloadData];}#pragma - DzyImgDelegate-
(void)getImages:(NSArray*)imgData{//此处返回的是选择图片的数组}- (void)viewDidLoad {
[superviewDidLoad];self.view.backgroundColor =
[UIColorwhiteColor];//此处需要注意 自己计算一下 我设置的每个cell 是60*60 间距10 所以 这里一般是设置
全屏宽度 如有特殊需求自行修改DzyView = [[DzyImgPicker alloc]
initWithFrame:CGRectMake(0,160, DzyWid,200)
andParentV:selfandMaxNum:9]; DzyView.dzyImgDelegate =self;
DzyView.backgroundColor = [UIColororangeColor]; [self.view
addSubview:DzyView]; __weaktypeof(self)weakSelf =self; [DzyView
setDzyImgs:^(NSArray*data) {//此处返回的是选择图片的数组}];// Do any additional setup
after loading the view.}
直接上代码看一下封装过程 DzyImgPicker.h
#import@protocolDzyImgDelegate//
代理方式返回数组-
(void)getImages:(NSArray*)imgData;@end@interfaceDzyImgPicker:UIView//初始化
添加父类view进去- (instancetype)initWithFrame:(CGRect)frame
andParentV:(UIViewController*)parentV
andMaxNum:(NSInteger)maxNum;//处理block
返回的图片@property(nonatomic)void(^DzyImgs)(NSArray*data);//从选择器返回的时候需要刷新界面
所以在每次界面将要显示的时候刷新用
@property(nonatomic)UICollectionView*collectionView;@property(nonatomic)id
dzyImgDelegate;@end
DzyImgPicker.m
#import"DzyImgPicker.h"#import"LGPhoto.h"#import"MLPhotoBrowserViewController.h"#import"imageCell.h"@interfaceDzyImgPicker()
{//存储图片NSMutableArray*imageArray;}//最大的图片张数
@property(nonatomic)NSIntegermaxNum;//父类的
view@property(nonatomic)UIViewController*parentV;@property(nonatomic,assign)
LGShowImageType showType;@end@implementationDzyImgPicker#pragma mark --
(NSInteger)
numberOfSectionInPhotosInPhotoBrowser:(MLPhotoBrowserViewController
*)photoBrowser{return1;}- (NSInteger)
photoBrowser:(MLPhotoBrowserViewController *)photoBrowser
numberOfItemsInSection:(NSUInteger)section{returnimageArray.count;}#pragma
mark - 每个组展示什么图片,需要包装下ZLPhotoPickerBrowserPhoto- (MLPhotoBrowserPhoto
*)photoBrowser:(MLPhotoBrowserViewController *)photoBrowser
photoAtIndexPath:(NSIndexPath*)indexPath{ MLPhotoBrowserPhoto *photo =
[MLPhotoBrowserPhoto photoAnyImageObjWith:imageArray[indexPath.row]];
imageCell *cell = (imageCell *)[_collectionView
cellForItemAtIndexPath:indexPath]; photo.toView = cell.imgView;
photo.thumbImage = cell.imgView.image;returnphoto;}#pragma mark -#pragma
mark 删除照片调用- (void)photoBrowser:(MLPhotoBrowserViewController
*)photoBrowser
removePhotoAtIndexPath:(NSIndexPath*)indexPat{if(imageArray.count ==1)
{ [imageArray removeAllObjects]; }else{ [imageArray
removeObjectAtIndex:indexPat.row]; [self.collectionView
reloadData]; }}#pragma mark - setupCell click
ZLPhotoPickerBrowserViewController- (void) setupPhotoBrowser:(imageCell
*) cell{NSIndexPath*indexPath = [self.collectionView
indexPathForCell:cell];// 图片游览器MLPhotoBrowserViewController
*photoBrowser = [[MLPhotoBrowserViewController alloc] init];//
缩放动画photoBrowser.status =UIViewAnimationAnimationStatusZoom;//
可以删除photoBrowser.editing =YES;// delegatephotoBrowser.delegate =self;//
数据源photoBrowser.dataSource =self;// 当前选中的值photoBrowser.currentIndexPath =
[NSIndexPathindexPathForItem:indexPath.row inSection:0];//
展示控制器[photoBrowser show];}/**
* 初始化自定义相机(连拍)
*/#pragma mark
- *初始化自定义相机(连拍)- (void)presentCameraContinuous {
ZLCameraViewController *cameraVC = [[ZLCameraViewController alloc]
init];// 拍照最多个数cameraVC.maxCount =self.maxNum-imageArray.count;//
连拍cameraVC.cameraType = ZLCameraContinuous; cameraVC.callback =
^(NSArray*cameras){//在这里得到拍照结果//数组元素是ZLCamera对象for(ZLCamera
*canamerincameras) { [imageArray
addObject:canamer.photoImage];
}NSLog(@"%lu",(unsignedlong)imageArray.count);
};//展示在父类的View上[cameraVC showPickerVc:self.parentV];
[self.collectionView reloadData];}#pragma mark -
*LGPhotoPickerViewControllerDelegate-
(void)pickerViewControllerDoneAsstes:(NSArray*)assets
isOriginal:(BOOL)original{//thumbImage (缩略图) fullResolutionImage(全屏图)
原图(originImage)NSMutableArray*originImage =
[NSMutableArrayarray];for(LGPhotoAssets *photoinassets)
{//原图[originImage addObject:photo.originImage]; [imageArray
addObject:photo.originImage]; } [self.collectionView
reloadData];}#pragma mark - * 初始化相册选择器-
(void)presentPhotoPickerViewControllerWithStyle:(LGShowImageType)style
{ LGPhotoPickerViewController *pickerVc =
[[LGPhotoPickerViewController alloc] initWithShowType:style];
pickerVc.status = PickerViewShowStatusCameraRoll; pickerVc.maxCount
=self.maxNum-imageArray.count;//设置选择张数上线pickerVc.delegate
=self;self.showType = style;//展示在父类的view上[pickerVc
showPickerVc:self.parentV];}#pragma mark *UIActionsheetDelegate-
(void)actionSheet:(UIActionSheet*)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex{NSString*msg =
[NSStringstringWithFormat:@"选取图片最大为%ld张!",
(long)self.maxNum];switch(buttonIndex) {case0:
{NSLog(@"相机");if(self.maxNum-imageArray.count ==0) {UIAlertView*al =
[[UIAlertViewalloc] initWithTitle:@"提示"message:msg
delegate:nilcancelButtonTitle:nilotherButtonTitles:@"知道了",nil];
[al show]; }else{
[selfpresentCameraContinuous]; } }break;case1:
{NSLog(@"相册");if(self.maxNum-imageArray.count ==0) {UIAlertView*al =
[[UIAlertViewalloc] initWithTitle:@"提示"message:msg
delegate:nilcancelButtonTitle:nilotherButtonTitles:@"知道了",nil];
[al show]; }else{
[selfpresentPhotoPickerViewControllerWithStyle:LGShowImageTypeImagePicker];
} }break;case2: {NSLog(@"取消");
}break;default:break; }}#pragma mark *UIActionSheet-
(void)toChoose{UIActionSheet*actionSheet = [[UIActionSheetalloc]
initWithTitle:nildelegate:selfcancelButtonTitle:@"取
消"destructiveButtonTitle:nilotherButtonTitles:@"相机",@"相册",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.parentV.view];}-
(void)createUI{UICollectionViewFlowLayout*flow =
[[UICollectionViewFlowLayoutalloc] init]; _collectionView =
[[UICollectionViewalloc] initWithFrame:self.bounds
collectionViewLayout:flow];//设置代理_collectionView.backgroundColor =
[UIColorwhiteColor];#warning mark 此处需要注意当正式使用的时候需要将边去掉
我这里是方便测试的时候显示范围_collectionView.layer.borderColor =
[[UIColorredColor]CGColor]; _collectionView.layer.borderWidth =0.5;
_collectionView.delegate =self; _collectionView.dataSource
=self;//注册cell和ReusableView(相当于头部)[self.collectionView
registerClass:[imageCellclass]
forCellWithReuseIdentifier:@"DzyImgCell"];
[selfaddSubview:_collectionView];}-
(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{return1;}-
(NSInteger)collectionView:(UICollectionView*)collectionView
numberOfItemsInSection:(NSInteger)section{//------ block
方式漏出图片组供使用if(_DzyImgs) { _DzyImgs(imageArray); }//-------
代理方式if(self.dzyImgDelegate && [self.dzyImgDelegate
respondsToSelector:@selector(getImages:)]) { [self.dzyImgDelegate
getImages:imageArray]; }//这里边设置 的是 点击添加图片按钮 如果没有到最上限 一直限制添加按钮
否则隐藏掉if(imageArray.count ==self.maxNum) {returnself.maxNum;
}else{returnimageArray.count+1; }}-
(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath*)indexPath{ imageCell *cell =
[collectionView
dequeueReusableCellWithReuseIdentifier:@"DzyImgCell"forIndexPath:indexPath];if(!cell)
{// NSLog(@"不会进入");}if(indexPath.row == imageArray.count) {//
NSLog(@"判断是最后一个 是添加按钮");#warning mark 更改添加按钮背景图片cell.imgView.image =
[UIImageimageNamed:@"plus23.png"]; cell.imgView.contentMode
=UIViewContentModeScaleAspectFit;
}else{//从相册或者相机选择的图片cell.imgView.image = imageArray[indexPath.row];
cell.imgView.contentMode =UIViewContentModeScaleToFill; }#warning
mark 修改背景颜色cell.imgView.backgroundColor =
[UIColorredColor];returncell;}#pragma mark
--UICollectionViewDelegate//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView*)collectionView
didSelectItemAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.row ==
imageArray.count && indexPath.row
实现代码我就放在github上了Demo有朋友发现可优化更新的部分请联系我
简单集成高大上的ImagePicker无标题文章的更多相关文章
- S005SELinux(SEAndroid)的实际文件组成无标题文章
SEAndroid 是将SELinux 移植到Android 上的产物,可以看成SELinux 辅以一套适用于Android 的策略. 那么在android系统中那些文件是与SELinux(SEAnd ...
- 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动
作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...
- android ActionBarActivity设置全屏无标题
新建的Activity继承自ActionBarActivity,设置全屏无标题本来很简单的事,但是没想到app竟然无缘无故的挂,要么就是白屏一片,要么就是黑屏.坑了我一个多小时!!! 原因是Actio ...
- MFC修改窗口无标题和标题信息,修改执执行文件图标
一.创建MFC后 窗口显示的是 无标题-工程名 修改方法在网上看到了几种,下面介绍下比较简单的一种: 1.在MianFrame.c文件中找到这个函数 BOOL CMainFrame::PreCreat ...
- Android中无标题样式和全屏样式学习
在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManife ...
- 超简单集成华为HMS ML Kit文本识别SDK,一键实现账单号自动录入
前言 在之前的文章<超简单集成华为HMS Core MLKit通用卡证识别SDK,一键实现各种卡绑定>中我们给大家介绍了华为HMS ML Kit通用卡证识别技术是如何通过拍照自动识别卡 ...
- 超简单集成 HMS ML Kit 实现最大脸微笑抓拍
前言 如果大家对 HMS ML Kit 人脸检测功能有所了解,相信已经动手调用我们提供的接口编写自己的 APP 啦.目前就有小伙伴在调用接口的过程中反馈,不太清楚 HMS ML Kit 文档中的 ML ...
- [WPF] 我的WPF自学日记1,无标题窗体拖动
学习WPF的第一天,尝试写比较常用的功能,无标题窗体拖动. 先在设计界面给它加上MouseDown事件 <Window x:Class="MyFirstWPFAPP.MainWindo ...
- windows程序里新窗体不在任务栏显示,无标题拖动,键盘事件,始终显示在主窗体上面,单实例运行等
不显示在任务栏,Alt+Tab也不显示 protected override CreateParams CreateParams { get { const int WS_EX_APPWINDOW = ...
随机推荐
- 移植QT5.6到嵌入式开发板(史上最详细的QT移植教程)
目前网上的大多数 QT 移植教程还都停留在 qt4.8 版本,或者还有更老的 Qtopia ,但是目前 Qt 已经发展到最新的 5.7 版本了,我个人也已经使用了很长一段时间的 qt5.6 for w ...
- 进程控制块(PCB)结构
一.进程控制块(PCB)结构 进程控制块(PCB)是系统为了管理进程设置的一个专门的数据结构.系统用它来记录进程的外部特征,描述进程的运动变化过程.同时,系统可以利用PCB来控制和管理进程,所以说,P ...
- json 数组操作
用js有很久了,但都没有深究过js的数组形式.这段时间做的一个项目,用到数组的地方很多,自以为js还可以的自己居然无从下手,一下狠心,我学!呵呵. 1.数组的创建 var arrayObj = new ...
- grafana-zabbix图形简单配置
连接zabbix数据库 加入dashboard Home--Add--加入dashboad 设置dashboad 设置名字,和标签tag,tag可在输入后回车加入多个 加入简单的一张图,測试能否获取到 ...
- 【笔记】让360浏览器用chrome 内核渲染你的网页
学校的项目还处在测试阶段 有一个痛点就是有一些页面在360浏览器中默认以ie 内核渲染 这样很不好 以为部分页面因技术方面的不足导致并不能很好地兼容ie 浏览器,于是在网上找了一下答案 可真还有解决方 ...
- 从HttpServletRequest获取完整的请求路径
String url = request.getRequestURI(); // 这个方法只能获得不包含参数的请求url,且只包含相对路径 StringBuffer url_buffer = requ ...
- PHP快速入门 如何下载和安装
1 从该网站下载 http://www.appservnetwork.com/ 2 双击安装,注意安装路径和安装组件 按下面的填写,端口不要和IIS冲突,字符编号为UTF-8 安装完成之后,注意安装文 ...
- Grunt的配置及使用(压缩合并js/css)
Grunt的配置及使用(压缩合并js/css) 安装 前提是你已经安装了nodejs和npm. 你能够在 nodejs.org 下载安装包安装.也能够通过包管理器(比方在 Mac 上用 homebre ...
- Unity骨骼动力学应用
原地址:http://blog.csdn.net/libeifs/article/details/7169794 开发环境 Window7 Unity3D 3.4.1 MB525defy Andro ...
- ubuntu server激活即时通讯IM服务 Instant Messaging is not activated on this server
转自:http://shine-it.net/index.php/topic,16469.msg28364.html ubuntu server 下 odoo激活及时通讯功能im OpenERP IM ...