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(),等

UIImageView与基本动画gif的更多相关文章

  1. UIImageView的animationImages动画

    UIImageView的animationImages动画 UIImageView的animationImages,只有在做非常规动画的时候才有优势,比方说下图中左侧动画.如果用来做下图中的右侧动画, ...

  2. UIImageView 自带动画+N张图片实现很炫的动画

    gitHub上又看到个很炫的动画:https://github.com/MartinRGB/GiftCard-iOS   看了看他的代码,发现核心动画(就是把按钮包装成一个礼物盒)其实很简单,就是把一 ...

  3. uiimageView连续帧动画

    // //  MJViewController.m //  05-汤姆猫 // //  Created by apple on 14-3-24. //  Copyright (c) 2014年 itc ...

  4. iOS开发之--UIImageView的animationImages动画

    图片动画实现,代码如下: -(UIImageView *)animationImageView { if (!_animationImageView) { _animationImageView= [ ...

  5. IOS UIImageView的帧动画

    ● UIImageView可以让一系列的图片在特定的时间内按顺序显示 ● 相关属性解析: ● animationImages:要显示的图片(一个装着UIImage的NSArray) ● animati ...

  6. [Xcode 实际操作]六、媒体与动画-(13)使用UIImageView制作帧动画

    目录:[Swift]Xcode实际操作 本文将演示如何将导入的序列图片,转换为帧动画. 在项目导航区打开资源文件夹[Assets.xcassets] [+]->[Import]->选择图片 ...

  7. UIImageView的序列帧动画

    #pragma mark - 开始动画 - (IBAction)startAnimation { // 1.1 加载所有的图片 NSMutableArray<UIImage *> *ima ...

  8. uiimageview 的 animation 动画

    NSMutableArray *meiArr = [NSMutableArray arrayWithCapacity:4]; for (int i = 0; i < 4; i++) { NSSt ...

  9. UIImageView 动画 / UIImage 方向

    UIImage 方向 UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向 如果本身是横屏, 照片也是横屏的话, 方向是正方向 BOOL b1 = (originalIma ...

随机推荐

  1. Tornado 异步socketTCP通信

    Tornado 有 TCPClient 和 TCPServer 两个类,可用于实现 tcp 的客户端和服务端.事实上,这两个类都是对iostream的简单包装. 真正重要的是 iostream ios ...

  2. 人人都懂区块链--pdf电子版学习资料下载

    人人都懂区块链 21天从区块链“小白”到资深玩家电子版pdf下载 链接:https://pan.baidu.com/s/1TWxYv4TLa2UtTgU-HqLECQ 提取码:6gy0 好的学习资料需 ...

  3. hdu 1533 Going Home (KM)

    Going HomeTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. python:0

    if __name__ == '__main__': r = Rectangle() 79 def __str__(self): 80 return "address:(%d, %d)&qu ...

  5. 学习记录:《C++设计模式——李建忠主讲》4.“单一职责”模式

    单一职责模式:在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式:装饰模式(Decorator).桥 ...

  6. 新闻实时分析系统 Spark2.X集群运行模式

    1.几种运行模式介绍 Spark几种运行模式: 1)Local 2)Standalone 3)Yarn 4)Mesos 下载IDEA并安装,可以百度一下免费文档. 2.spark Standalone ...

  7. JavaScript笔记二

    1.表格 - 在网页中可以通过表格来表示一些格式化的数据 - 表格相关的标签 - <table> 用来创建一个表格 - <tr> 表示表格中的一行 - <th> 表 ...

  8. String字符串为什么不可变的深入理解

    String是被final修饰的,是不可变对象,那么这句什么意思呢.在学习scala时候var,val时候,就想到这个问题,所以记录下 看案例: package com.cxy; import sun ...

  9. linux basic

    一:date 语法: 打印日期:date [OPTION].....  [+FORMAT] 设定日期:date [MMDDhhmm] [[cc][YY][.ss] 创建带实时日期的文件 touch $ ...

  10. 【Java实例】使用Thumbnailator生成缩略图(缩放、旋转、裁剪、水印)

    1 需求 表哥需要给儿子报名考试,系统要求上传不超过30KB的图片,而现在的手机随手一拍就是几MB的,怎么弄一个才30KB的图片呢? 一个简单的办法是在电脑上把图片缩小,然后截屏小图片,但现在的电脑屏 ...