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. ARM7中断的理解

    谈谈对中断的理解?   中断是计算机中处理异步事件的重要机制      中断触发的方式:       1)中断源级设置          按键:(CPU之外的硬件)               设置中 ...

  2. python函数的基本语法<二>

    函数的流程控制: if...else... a = 100 b = 200 if a == 100 and b ==300: print('100,200') elif b == 200: print ...

  3. 201871010114-李岩松《面向对象程序设计(java)》第七周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  4. 总结:mysql的各种增删改查!

    (原创总结)分为数据库的增删改查,数据表(和字段)的增删改查,数据的增删改查 三部分!// 创建用户并授权 GRANT SELECT ON bodydb.user TO us@localhost ID ...

  5. vue使用talkIngData统计

    上篇刚使用了百度统计,由于后端同事没有找到百度统计的api,于是就找了个talkingData平台... 平台地址 依旧是先注册 进入应用统计分析  创建应用 获得appid 在vue中埋点 1.in ...

  6. BloomFilter在Hudi中的应用

    Bloom Filter在Hudi中的应用 介绍 Bloom Filter可以用于检索一个元素是否在一个集合中.它的优点是空间效率和查询时间都远远超过一般的算法,主要缺点是存在一定的误判率:当其判断元 ...

  7. 【论文阅读】The Contextual Loss for Image Transformationwith Non-Aligned Data(ECCV2018 oral)

    目录: 相关链接 方法亮点 相关工作 方法细节 实验结果 总结与收获 相关链接 论文:https://arxiv.org/abs/1803.02077 代码:https://github.com/ro ...

  8. [译]Nginx入门引导教程

    本文为[Beginner's Guide]译文,原文地址:http://nginx.org/en/docs/beginners_guide.html Guide 本教程基础的介绍了 nginx,以及能 ...

  9. JavaScript笔记五

    1.条件分支语句 - switch语句 - 语法: switch(条件表达式){ case 表达式: 语句... break; case 表达式: 语句... break; case 表达式: 语句. ...

  10. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...