简单集成高大上的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 = ...
随机推荐
- [Android 新特性] Android 4.3 Top 5新功能
直播会上,Android 4.3不像Nexus 7那么Sexy,也没有Chromecast电视棒那样惹人注目,但它有许多强悍的新特点,下面来看看Top 5. 1. 权限配置 支持在一台终端设备上有多个 ...
- 关于QtCharts中的映射器与模型的使用
简述 本文章基于博主在使用QtCharts中一些经验总结,相关了Qt类有QVXYModelMapper,CustomTableModel(一个继承了QAbstractTableModel的类,用于实现 ...
- django 实现上传文件功能
需求:自己写一个文件上传功能 代码: urls.py from django.conf.urls import url from django.contrib import admin from ap ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- 倾斜摄影三维建模软件photoscan教程 [转]
PhotoScan是一款基于影响自动生成高质量三维模型的优秀软件,这对于3D建模需求来说实在是一把利器. PhotoScan无需设置初始值,无须相机检校,它根据最新的多视图三维重建技术,可对任意照片进 ...
- [Webpack] Create Separate webpack Configs for Development and Production with webpack-merge
The development and production modes in webpack optimize the output in different ways. In developmen ...
- asp与aspx有什么区别
asp与aspx哪个好? 最佳答案 你如果想知道哪个好,我想很多人第一个想法就是aspx好. 这很好理解,就像楼上的朋友说的,aspx是asp的升级版. 但是aspx相对于asp的升级可以说是从根本上 ...
- taro 填坑之路(二)taro 通过事件监听 实现组件间传值
1.组件传值的方式 2.事件监听原理 3.事件管理器 utils/event.js /** * 事件池(事件管理器) * 通过事件监听传值 */ class Event { constructor() ...
- 使用 Tmux 强化终端功能
来自 tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需 ...
- Some web Address
1.可视化算法(Data Structure Visualizations) https://www.cs.usfca.edu/~galles/visualization/Algorithms.htm ...