UIImageView与基本动画gif
UIImageView 作用:[用来进行图片展示]
UIImageView
UIImageView初始化
initWithImage:如果设置frame,图片的size就是 imageView的size
UIImage的初始化方法
1、imageNamed:方法
imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些。它的加载流程如下:
a. 系统会去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。
b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。
观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那 种频繁用到界面贴图类的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。
imageView支持的图片: 首选png,jpg
2、imageWithContentsOfFile:和imageWithData:方法
图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通 过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。
//两个将图片转换成data的方法(比如:服务器端上传头像,图片缓存数据库)
NSData * __nullable UIImagePNGRepresentation(UIImage * __nonnull image)
//可以进行压缩来操作
NSData * __nullable UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality);
设置圆角和边框颜色
(1)设置圆角
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 10;
(2)设置边框颜色和大小
imageView.layer.borderColor = [UIColor orangeColor].CGColor;
imageView.layer.borderWidth = 2;
contentMode属性(Demo)
这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。 UIViewContentModeScaleToFill属性会导致图片变形,图片填充满frame(默认)。
UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。 UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。(有一部分会被裁切掉)
图片拉伸(resizedImage:)(Demo:)
UIImage的图片拉伸方法
stretchableImageWithLeftCapWidth:topCapHeight:
leftCapWidth ,是左侧需要保留的像素数,topCapHeight是顶部需要保留的像素数,然后中间的1像素用于中间的平铺,达到最后所需 要的尺寸。效果相当于只能保持一边固定,拉伸另一边。
注意:只是对一个像素进行复制到一定宽度。而图像后面的剩余像素也不会被拉伸。
图片动画处理
1.基本动画常用API:
-isAnimation:
-startAnimation:
-stopAnimation:
-setAnimationImages:
-setAnimationDuration:
-setAnimationRepeatCount:
2.使用一张GIF图,完成动画(需要使用CoreText框架,需要导入ImageIO框架)
GIF ==> Data ==> 获取gif帧数(总数量) ==> 获取指定帧的图像 ==> 获取指定图像的属性 ==> 获取属性中的GIF属性 ==> 找出持续时间 ==> 拿到总时间 ==> 设置iamgeView的基本属性完成动画
//Create an image source reading from `data'
CGImageSourceRef __nullable CGImageSourceCreateWithData(CFDataRef __nonnull data, CFDictionaryRef __nullable options)
/* Return the number of images (not including thumbnails) in the image
* source `isrc'. */
size_t CGImageSourceGetCount(CGImageSourceRef __nonnull isrc)
/* Return the image at `index' in the image source `isrc'. The index is
zero-based. */
CGImageRef __nullable CGImageSourceCreateImageAtIndex(CGImageSourceRef __nonnull isrc, size_t index, CFDictionaryRef __nullable options)
/* Return the properties of the image source `isrc’.*/
CFDictionaryRef __nullable CGImageSourceCopyProperties(CGImageSourceRef __nonnull isrc, CFDictionaryRef __nullable options)
//获取指定的关于GIF属性:(const CFStringRef kCGImagePropertyGIFDictionary)
*frameProperties = [properties:( *)kCGImagePropertyGIFDictionary];
//获取图片的持续时间(const CFStringRef kCGImagePropertyGIFDelayTime)
NSNumber *lastTime = [frameProperties objectForKey:(NSString *) kCGImagePropertyGIFDelayTime];
注意内存管理:Core核心框架的对象需要使用对应的release方式,ARC无法对Core核心框架的对象做管理
CFRelease(),CGImageRelease(),等