目录
1. UIImageView 图片
2. SDWebImage 加载网络图片
3. 图片压缩
4. SDCycleScrollView 轮播图
5. TZImagePickerController 图片选择器
1. UIImageView 图片(: UIView )
// 创建
UIImageView *imgV=[UIImageView new];
[self.view addSubview:imgV];
[imgV autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
常用
// 设置 图片
[imgV setImage:[UIImage imageNamed:@""]];
// 设置 图片模式
[imgV setContentMode:UIViewContentModeScaleAspectFill];
/*
缩放
UIViewContentModeScaleToFill, 默认
UIViewContentModeScaleAspectFit, 不会裁剪(会有黑边)
UIViewContentModeScaleAspectFill, 会裁剪
不缩放
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
继承UIView所拥有:
// 设置 可交互(默认:不可交互)
[imgV setUserInteractionEnabled:true];
// 设置 是否剪裁
[imgV setClipsToBounds:true];
// 设置 边框色、边框宽度、是否剪裁
[imgV.layer setBorderColor:[UIColor redColor].CGColor];
[imgV.layer setBorderWidth:1.0];
[imgV.layer setMasksToBounds:true];
// 圆角
[imgV.layer setCornerRadius:30]; // 宽高一半则为圆
[imgV.layer setMasksToBounds:true];
帧动画
// 设置 数据源
NSArray *imgArr=@[[UIImage imageNamed:@""]];
[imgV setAnimationImages:imgArr];
// 设置 持续时间
[imgV setAnimationDuration:1.25];
// 设置 重复次数
[imgV setAnimationRepeatCount:CGFLOAT_MAX];
// 开始动画
[imgV startAnimating];
// 停止动画
[imgV stopAnimating];
// 获取 是否正在动画
BOOL isAni=imgV.isAnimating;
UIImage
动态加载
只用填写图片名,会动态选择@2x或@3x进行加载图片
获取本地图片
// 方式1:首先会在缓存中查找,如果没找到则加载并缓存。适用于频繁使用的图片。
UIImage *img=[UIImage imageNamed:@""];
// 方式2:不会缓存图片,适用于较大的图片、使用情况较少。
UIImage *img2=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"1.jpg" ofType:nil]];
UIImage *img3=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"]];
获取网络图片
// 方式1:通过NSdata创建时,UIImage 底层是调用 ImageIO 的 CGImageSourceCreateWithData() 方法。该方法有个参数叫 ShouldCache,在 64 位的设备上,这个参数是默认开启的。这个图片也是同样在第一次显示到屏幕时才会被解码,随后解码数据被缓存到 CGImage 内部。与 imageNamed 创建的图片不同,如果这个图片被释放掉,其内部的解码数据也会被立刻释放。
UIImage *img3=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://.../1.jpg"]]];
// 方式2:
// SDWebImage
获取照片
#import
#import
#import
// [self getPhoto];
-(void)getPhoto{
UIAlertController *alertAC=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
[alertAC addAction:[UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
[self getPhoto:0];
}]];
[alertAC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self getPhoto:1];
}]];
[alertAC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertAC animated:true completion:^{
}];
}
-(void)getPhoto:(int)index{
// 创建UIImagePickerController
UIImagePickerController *imgPickC=[UIImagePickerController new];
imgPickC.delegate=self;
imgPickC.allowsEditing=true; // 允许获取图片时编辑图片
switch (index) {
case 0:{
//
if(![self isCanUsePhotos]){
[self showAlertView];
}else{
//
imgPickC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imgPickC animated:true completion:^{
}];
}
}
break;
case 1:{
//
if(![self isCanUseCamera]){
[self showAlertView];
}else{
imgPickC.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imgPickC animated:true completion:^{
}];
}
}
break;
default:
break;
}
}
// 是否 有使用相册权限
- (BOOL)isCanUsePhotos{
// if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
ALAuthorizationStatus authStatus =[ALAssetsLibrary authorizationStatus];
if (authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied) {
// 无权限
return false;
}
}else {
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if (authStatus == PHAuthorizationStatusRestricted || authStatus == PHAuthorizationStatusDenied) {
// 无权限
return false;
}
}
return YES;
}
// 是否 有使用相机权限
-(BOOL)isCanUseCamera{
// if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
AVAuthorizationStatus authStatus=[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusRestricted || authStatus==AVAuthorizationStatusDenied){
return false;
}
return true;
}
-(void)showAlertView{
// 无权限
UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"无法获取相机权限,去设置中心打开权限" preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[alertC addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"App-Prefs:root=Photos"]]];
}]];
[self presentViewController:alertC animated:true completion:^{
}];
return;
}
// dele
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//
[picker dismissViewControllerAnimated:YES completion:nil];// 销毁控制器
UIImage *image = info[UIImagePickerControllerOriginalImage];// 获得图片
NSData *imageDta=[UIImage representationImage:image];
}
图片格式
GIF 诞生于 1987 年
它有很多缺点,比如通常情况下只支持 256 种颜色、透明通道只有 1 bit、文件压缩比不高。它唯一的优势就是支持多帧动画
JPEG 诞生于 1992 年(是目前最常见的图片格式)
它只支持有损压缩,其压缩算法可以精确控制压缩比,以图像质量换得存储空间。
PNG 诞生在 1995 年
它本身的设计目的是替代 GIF 格式,所以它与 GIF 有更多相似的地方。
PNG 只支持无损压缩,所以它的压缩比是有上限的。相对于 JPEG 和 GIF 来说,它最大的优势在于支持完整的透明通道。
APNG 是 Mozilla 在 2008 年发布的一种图片格式,旨在替换掉画质低劣的 GIF 动画。
WebP 是 Google 在 2010 年发布的图片格式,希望以更高的压缩比替代 JPEG。它用 VP8 视频帧内编码作为其算法基础,取得了不错的压缩效果。它支持有损和无损压缩、支持完整的透明通道、也支持多帧动画,并且没有版权问题,是一种非常理想的图片格式。
BPG 是著名程序员 Fabrice Bellard 在去年 (2014年) 发布的一款超高压缩比的图片格式。BPG 使用 HEVC (即 H.265) 帧内编码作为其算法基础,当下最为先进的图片压缩格式.
Android 的图片编码解码是由 Skia 图形库负责的,Skia 通过挂接第三方开源库实现了常见的图片格式的编解码支持。目前来说,Android 原生支持的格式只有 JPEG、PNG、GIF、BMP 和 WebP (Android 4.0 加入),在上层能直接调用的编码方式也只有 JPEG、PNG、WebP 这三种。目前来说 Android 还不支持直接的动图编解码。
iOS 底层是用 ImageIO.framework 实现的图片编解码。目前 iOS 原生支持的格式有:JPEG、JPEG2000、PNG、GIF、BMP、ICO、TIFF、PICT,自 iOS 8.0 起,ImageIO 又加入了 APNG、SVG、RAW 格式的支持。在上层,开发者可以直接调用 ImageIO 对上面这些图片格式进行编码和解码。对于动图来说,开发者可以解码动画 GIF 和 APNG、可以编码动画 GIF。
图片拉伸(类似 android点9图)
[[UIImage imageNamed:@"exportDetailHeadBG"]resizableImageWithCapInsets:UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
CapInsets:图片上左下右不拉伸的距离,用剩下的部分拉伸
resizingMode:UIImageResizingModeStretch 拉伸、UIImageResizingModeTile 填充
2. SDWebImage 加载网络图片
- 简介
SDWebImage原理:
首先从内存中查找,如果找到则进行显示、如果没有找到则从沙盒中进行查找;如果沙盒中找到则添加到内存中并显示、如果没找到则从后台下载到沙盒,并添加到内存中,再进行显示。
支持GIF动画、主线程不会被阻塞(异步下载)、相同的url不会被下载多次
所有sd_setImageWithURL最终都会加载如下方法(常用手法)
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock{
该方法调用私有方法sd_internalSetImageWithURL
}
私有方法sd_internalSetImageWithURL
取消正加载的图片地址
NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
待续。。。
- 使用
Podfile中+
# SDwebImage
pod 'SDWebImage'
UIImageView
#import
[imgV sd_setImageWithURL:[NSURL URLWithString:@""]];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage new]];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(这是通常的做法)
[imgV sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 动画
[imgV sd_setAnimationImagesWithURLs:@[[NSURL URLWithString:@""]]];
// 取消当前动画
[imgV sd_cancelCurrentAnimationImagesLoad];
/*
参数说明:
url : 图片地址
completed : 显示完毕后调用
placeholderImage : 占位图(图片无法获取时显示)
options:
SDWebImageRetryFailed(下载失败后重试)
SDWebImageLowPriority(交互时就下载,影响流畅)
SDWebImageCacheMemoryOnly(只进行内存缓存,不在沙盒中缓存)
SDWebImageProgressiveDownload(渐进式下载,边下载边显示)
SDWebImageRefreshCached(刷新缓存,若地址改变图片不变则使用)
SDWebImageContinueInBackground(后台下载)
SDWebImageHandleCookies
SDWebImageAllowInvalidSSLCertificates(允许使用无效SSL)
SDWebImageHighPriority(优先下载)
SDWebImageDelayPlaceholder
SDWebImageTransformAnimatedImage
*/
UIButton
#import "UIButton+WebCache.h"
图
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed];
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(常见封装方式)
[completeButton sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
背景图
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed];
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
// 以上方法都会调用如下方法(常见封装方式)
[completeButton sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
SDWebImageDownLoader
#import "SDWebImageDownloader.h"
// 下载图片
SDWebImageDownLoader *downLoader=[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:@""] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
};
/*
参数说明
options:
SDWebImageDownloaderLowPriority 低优先级
SDWebImageDownloaderProgressiveDownload 下载过程中逐步显示
SDWebImageDownloaderUseNSURLCache 使用NSURLCache
SDWebImageDownloaderIgnoreCachedResponse 组合SDWebImageDownloaderUseNSURLCache使用,当图片从缓存中获取时,completed中的image是nil
SDWebImageDownloaderContinueInBackground 支持后台下载
SDWebImageDownloaderHandleCookies 使用Cookies
SDWebImageDownloaderAllowInvalidSSLCertificates 支持不安全的SSL连接(用于测试)
SDWebImageDownloaderHighPriority 高优先级
SDWebImageDownloaderScaleDownLargeImages 缩放大图片
*/
缓存
#import "SDImageCache.h"
获取图片的缓存大小
[SDImageCache sharedImageCache] getSize];
清理缓存
清理内存缓存
[[SDImageCache sharedImageCache] clearMemory];
清理磁盘缓存
[[SDImageCache sharedImageCache] clearDisk];
//
SDImageCache *imgCache=[SDImageCache sharedImageCache];
缓存图片
// 缓存图片到沙盒
[imgCache storeImageDataToDisk:[NSData data] forKey:@""];
// 缓存图片到沙盒和内存
[imgCache storeImage:[UIImage imageNamed:@""] forKey:@"" completion:^{
}];
// 缓存图片到内存,是否沙盒
[imgCache storeImage:[UIImage imageNamed:@""] forKey:@"" toDisk:true completion:^{
}];
// 缓存图片到内存,是否沙盒
[imgCache storeImage:[UIImage imageNamed:@""] imageData:nil forKey:@"" toDisk:true completion:^{
}];
获取缓存图片
// 获取缓存图片(先从内存再从沙盒)
UIImage *img=[imgCache imageFromCacheForKey:@"key"];
// 获取缓存图片(从内存)
UIImage *img2=[imgCache imageFromMemoryCacheForKey:@"key"];
// 获取缓存图片(从沙盒)
UIImage *img3=[imgCache imageFromDiskCacheForKey:@"key"];
SDWebImageManager
#import
SDWebImageManager *mangeM=[SDWebImageManager sharedManager];
// 下载图片
[mangeM loadImageWithURL:[NSURL URLWithString:@""] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
}];
// 取消所有下载任务
[mangeM cancelAll];
// 是否有任务正在下载
BOOL isRun=[mangeM isRunning];
// 对图片地址url做筛选
[mangeM setCacheKeyFilter:^(NSURL *url) {
url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
return [url absoluteString];
}];
// 将图片(对应url)加入到缓存
[mangeM saveImageToCache:[UIImage new] forURL:[NSURL URLWithString:@""]];
// 检测图片是否存在于缓存(异步)
[mangeM cachedImageExistsForURL:[NSURL URLWithString:@""] completion:^(BOOL isInCache) {
}];
// 检测图片是否存在于沙盒(异步)
[mangeM diskImageExistsForURL:[NSURL URLWithString:@""] completion:^(BOOL isInCache) {
}];
// 缓存key
NSString *cacheKey=[mangeM cacheKeyForURL:[NSURL URLWithString:@""]];
// cache(readOnly)
SDImageCache *imageCache=mangeM.imageCache;
// (readOnly)
SDWebImageDownloader *imageDownloader=mangeM.imageDownloader;
dele
[mangeM setDelegate:self]; // SDWebImageManagerDelegate
// 是否下载图片(过滤)
-(BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL *)imageURL{
return true;
}
// 用image替换url
-(UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL{
return nil;
}
3. 图片压缩
方式一:压缩图片质量
// compression:0~1,值越小-质量越低-图片大小越小
NSData *data = UIImageJPEGRepresentation(image, compression);
UIImage *resultImage = [UIImage imageWithData:data];
说明:
1、不改变图片的分辨率。会尽可能保留图片清晰度,图片不会明显模糊。也因此当图片质量低于一定程度时,继续压缩不再有效果。
2、可通过data.length获取图片的大小(字节),循环压缩来达到指定大小
方式二:压缩图片尺寸
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), (NSUInteger)(resultImage.size.height * sqrtf(ratio)));
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
说明:
1、改变图片的尺寸
2、会改变图片的分辨率,大大降低图片的质量会使图片明显模糊(比压缩图片质量方式明显模糊)
实战
放在一个全局工具类中
先优先使用方式一压缩到一定程度,如未满足再使用方式二压缩。
具体代码见下
/**
压缩图片
@param image 要压缩的图片
@param maxLength 要压缩的字节大小 例:1024*1014*0.3333
@return 返回压缩后的图片二进制流
*/
+ (NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
// 1.如果图片满足指定大小,则返回。
NSData *data = UIImageJPEGRepresentation(image, 1);
if (data.length < maxLength) return data;
// 2.压缩质量
// 循环6次已经满足大多数情况。1/2^6==0.015625 100M的照片压缩成1.6M
// 压缩大小至 要压缩大小的0.9~1
CGFloat max = 1;
CGFloat min = 0;
CGFloat compression = 1; // 压缩比例
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
if (data.length < maxLength) return data;
// 3.压缩尺寸(压缩质量后图片大小仍大于指定大小)
UIImage *resultImage = [UIImage imageWithData:data]; // 压缩质量后的图片
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio)));
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, compression);
}
return data;
}
4. SDCycleScrollView 轮播图
pod 'SDCycleScrollView'
#import
// 创建(frame,dele,占位---没有图片数据源时显示的照片)
SDCycleScrollView * _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kMainScreenWidth,_bannerViewHeight) delegate:self placeholderImage:placeImage];
[self addSubview:_bannerView];
// 数据源---本地图片数组
[_bannerView setLocalizationImageNamesGroup:@[]];
// 数据源---网络图片数组
_bannerView.imageURLStringsGroup=@[];
// 数据源---每张图片标题
[_bannerView setTitlesGroup:@[]];
// 占位
_bannerView.placeholderImage = placeImage;
// 背景色
[_bannerView setBackgroundColor:kWhiteColor];
// imageMode
_bannerView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
// 清除缓存
[SDCycleScrollView clearImagesCache];
滚动 和 点击
// 滚动间隔时间(默认:2s)
[_bannerView setAutoScrollTimeInterval:2.0];
// 是否自动滚动(默认:true)
[_bannerView setAutoScroll:true];
// 是否无限循环
[_bannerView setInfiniteLoop:true];
// 滚动方向
[_bannerView setScrollDirection:UICollectionViewScrollDirectionVertical];
// 点击图片后回调(block监听---或者使dele去监听)
[_bannerView setClickItemOperationBlock:^(NSInteger currentIndex){
}];
// 图片滚动后回调
[_bannerView setItemDidScrollOperationBlock:^(NSInteger currentIndex){
}];
轮播文字
// 轮播文字 高
[_bannerView setTitleLabelHeight:15];
// 轮播文字 字体
[_bannerView setTitleLabelTextFont:[UIFont systemFontOfSize:18]];
// 轮播文字 颜色
[_bannerView setTitleLabelTextColor:[UIColor blueColor]];
// 轮播文字 背景颜色
[_bannerView setTitleLabelBackgroundColor:[UIColor whiteColor]];
圆点
// 是否显示圆点
_bannerView.showPageControl = NO;
// 是否隐藏单个圆点(默认:true)
_bannerView.hidesForSinglePage=true;
// 圆点 大小
[_bannerView setPageControlDotSize:CGSizeMake(10, 10)];
// 圆点 位置
[_bannerView setPageControlAliment:SDCycleScrollViewPageContolAlimentRight];
// 圆点 style
_bannerView.pageControlStyle=SDCycleScrollViewPageContolStyleClassic;
// 圆点 颜色
_bannerView.pageDotColor=kWhiteColor;
// 圆点 图片
_bannerView.pageDotImage=[UIImage imageNamed:@"HX_Banner_dot_nor"];
// 当前圆点 颜色
_bannerView.currentPageDotColor=kNavBarThemeColor;
// 当前圆点 图片
_bannerView.currentPageDotImage=[UIImage imageNamed:@"HX_Banner_dot_current"];
dele
#pragma 2
// 滚动后调用
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
}
// 选择照片后调用
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
}
- 自定义轮播cell
实现代理方法
-(Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view{
return [ZYDSlideCollectionViewCell class];
}
-(void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view{
//
ZYDSlideCollectionViewCell *slideCell=(ZYDSlideCollectionViewCell *)cell;
[slideCell setObject:self.listVM.headImageArray[index]];
}
// 这个方法在这里,仅是为了刷新CollectionView,初始化pageControl
[self.sliderHeadView setLocalizationImageNamesGroup:self.listVM.headImageArray];
5. TZImagePickerController 图片选择器(可多选图片/视频)
pod 'TZImagePickerController'
#import
创建
创建方式一:
// 最大选取数(默认:9),dele
TZImagePickerController *imgPicC=[[TZImagePickerController alloc]initWithMaxImagesCount:9 delegate:self];
创建方式二:
// 最大选取数,照片列数,dele
TZImagePickerController *imgPicC=[[TZImagePickerController alloc]initWithMaxImagesCount:9 columnNumber:4 delegate:self];
创建方式三:
// 最大选取数,照片列数,dele,是否需要push到照片选择页
TZImagePickerController *imgPicC=[[TZImagePickerController alloc]initWithMaxImagesCount:9 columnNumber:4 delegate:self pushPhotoPickerVc:true];
预览
TZImagePickerController *imgPicC=[[TZImagePickerController alloc]initWithSelectedAssets:@[] selectedPhotos:@[] index:0];
裁剪
TZImagePickerController *imgPicC=[[TZImagePickerController alloc]initCropTypeWithAsset:nil photo:[UIImage new] completion:^(UIImage *cropImage, id asset) {}];
// 几s未照片自动dismiss(默认:15s)
[pickerController setTimeout:8];
// 预览页图片的宽度(默认:600px)
[pickerController setPhotoPreviewMaxWidth:200];
// 是否允许选择原图(默认:true)
[pickerController setAllowPickingOriginalPhoto:true];
// 是否允许选择视频(默认:true)
[pickerController setAllowPickingVideo:true];
// 是否允许多选视频(默认:false)
[pickerController setAllowPickingMultipleVideo:true];
// 是否允许选择gif(默认:false)
[pickerController setAllowPickingGif:true];
// 是否允许选择图片(默认:true)
[pickerController setAllowPickingImage:true];
// 是否显示拍照按钮(默认:true)
[pickerController setAllowTakePicture:true];
// 是否显示预览页(默认:true)
[pickerController setAllowPreview:true];
// 是否允许自动消失(默认:true)
[pickerController setAutoDismiss:true];
剪裁
// 是否允许裁剪(默认:true,仅在单选按钮不显示时有效)
[pickerController setAllowCrop:false];
// 剪裁框的尺寸
[pickerController setCropRect:CGRectZero];
// 剪裁框的尺寸(竖屏)
[pickerController setCropRectPortrait:CGRectZero];
// 剪裁框的尺寸(横屏)
[pickerController setCropRectLandscape:CGRectZero];
// 是否圆形剪裁框
[pickerController setNeedCircleCrop:true];
// 圆形剪裁框的半径
[pickerController setCircleCropRadius:10];
// 剪裁框其他属性
[pickerController setCropViewSettingBlock:^(UIView *cropView){
}];
自定义图片/文本
// 自定义图片
[pickerController setTakePictureImageName:@""];
[pickerController setPhotoSelImageName:@""];
[pickerController setPhotoDefImageName:@""];
[pickerController setPhotoOriginSelImageName:@""];
[pickerController setPhotoOriginDefImageName:@""];
[pickerController setPhotoPreviewOriginDefImageName:@""];
[pickerController setPhotoNumberIconImageName:@""];
// 自定义文本
[pickerController setDoneBtnTitleStr:@""];
[pickerController setCancelBtnTitleStr:@""];
[pickerController setPreviewBtnTitleStr:@""];
[pickerController setFullImageBtnTitleStr:@""];
[pickerController setSettingBtnTitleStr:@""];
[pickerController setProcessHintStr:@""];
// 自定义颜色
[pickerController setOKButtonTitleColorNormal:[UIColor whiteColor]];
[pickerController setOKButtonTitleColorDisabled:[UIColor whiteColor]];
[pickerController setNaviBgColor:[UIColor whiteColor]];
[pickerController setNaviTitleFont:[UIFont systemFontOfSize:10]];
[pickerController setNaviTitleColor:[UIColor whiteColor]];
[pickerController setBarItemTextFont:[UIFont systemFontOfSize:10]];
[pickerController setBarItemTextColor:[UIColor whiteColor]];
dele (或者使用handle)
[pickerController setPickerDelegate:self]; //
// 选择gif后调用
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(id)asset{
}
// 选择视频后调用
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset{
}
// 选择照片后调用
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
}
// 选择后调用
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray *)infos{
}
// cancel后调用
-(void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker{
}
// 是否显示相册
-(BOOL)isAlbumCanSelect:(NSString *)albumName result:(id)result{
return true;
}
// 是否显示照片
-(BOOL)isAssetCanSelect:(id)asset{
return true;
}
handle
// 选择了视频后调用
[pickerController setDidFinishPickingVideoHandle:^(UIImage *coverImage,id asset){
}];
// 选择了图片后调用
[pickerController setDidFinishPickingPhotosHandle:^(NSArray *photos,NSArray *assets,BOOL isSelectOriginalPhoto){
}];
// 选择了gif后调用
[pickerController setDidFinishPickingGifImageHandle:^(UIImage *animatedImage,id sourceAssets){
}];
// cancel后调用
[pickerController setImagePickerControllerDidCancelHandle:^{
}];
// 选择后调用
[pickerController setDidFinishPickingPhotosWithInfosHandle:^(NSArray *photos,NSArray *assets,BOOL isSelectOriginalPhoto,NSArray *infos){
}];
// 单选模式下(即最大可选一张)是否显示选择按钮(默认:false)
[pickerController setShowSelectBtn:true];
// 设置最小选取数(默认:0)/最大选取数(默认:9)
[pickerController setMinImagesCount:0];
[pickerController setMaxImagesCount:9];
// 确认按钮是否一直可点击(无视最小选取数)
[pickerController setAlwaysEnableDoneBtn:true];
// true:按修改时间排序(默认),false:最新照片最前面
[pickerController setSortAscendingByModificationDate:true];
// 导出图片的宽度?
[pickerController setPhotoWidth:200];
// 设置用户已选择的照片,当再次点击时使用
[pickerController setSelectedAssets:[NSMutableArray new]];
// 设置小于图片宽度/高度100的不能选(默认:0)
[pickerController setMinPhotoWidthSelectable:100];
[pickerController setMinPhotoHeightSelectable:100];
// 是否隐藏不可选中的图片(默认:false)
[pickerController setHideWhenCanNotSelect:true];
// 是否使用原始图片
[pickerController setIsSelectOriginalPhoto:true];
// statusBar是否为黑色
[pickerController setIsStatusBarDefault:true];
// 设置返回按钮
[pickerController setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
}];