UIImageView

summary

UIImageView极其常用,功能比较专一:显示图片

pooperty

@property(nonatomic,retain) UIImage     *image;
显示的图片
@property(nonatomic,copy) NSArray *animationImages;
显示的动画图片
@property(nonatomic) NSTimeInterval animationDuration;
动画图片的持续时间
@property(nonatomic) NSInteger animationRepeatCount;
动画的播放次数(默认是0,代表无限播放)
@property(nonatomic) UIViewContentMode contentMode;
内容模式: 一般用来控制图片如何显示

Method

- (void)startAnimating; // 开始动画
- (void)stopAnimating; // 停止动画
- (BOOL)isAnimating; // 是否正在执行动画

UIImageView-设置imageView的frame

initWithImage
默认尺寸就是图片的尺寸,位置默认从(0,0)开始
imageView.frame = CGRectMake(100,100, image.size.width, image.size.height);
注意尺寸不能设置在图片之前(演示) 错误代码:imageView.frame.size.width = image.size.width
直接赋值size,但会出现OC语法错
原因:不能直接修改OC对象结构体属性的成员
结构体是值传递 如何赋值?
CGRect tempFrame = imageView.frame; // frame是一个新定义的变量
tempFrame = image.size;
imageView.frame = tempFrame; // 如果少了这一句(不是对象,是结构体) 常见写法
imageView.frame = (CGRect){CGPointMake(100,100), imageView.image.size};
imageView.frame = (CGRect){CGPointMakeZero, imageView.image.size};
修改frame的3种方式(同样适用于bounds/center)
  • 1.直接使用CGRectMake函数
  • 2.利用临时结构体变量
  • 3.直接运用结构体赋值

UIImage类

一个UIImage对象代表一张图片对象
  • Method

    加载图片的方式:
    1. imageNamed:
    2. imageWithContentsOfFile: 1. 加载Assets.xcassets这里面的图片:
    1> 打包后变成Assets.car
    2> 拿不到路径
    3> 只能通过imageNamed:来加载图片
    4> 不能通过imageWithContentsOfFile:来加载图片 2. 放到项目中的图片:
    1> 可以拿到路径
    2> 能通过imageNamed:来加载图片
    3> 也能通过imageWithContentsOfFile:来加载图片

    返回一张受保护且被拉伸的图片

    iOS 5.0以前使用(弃用)这个方法会自动计算出偏向中间的一个1*1的方格也就是被拉伸的地方(默认使用拉伸)

    [image stretchableImageWithLeftCapWidth:imageHeight *0.5 topCapHeight:imageHeight *0.5 ];

    新方法

    [image resizableImageWithCapInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
    //拉伸模式 UIImageResizingModeTile平铺 UIImageResizingModeStretch拉伸
    [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageheight * 0.5, imagewidth * 0.5, imageheight * 0.5 -1, imagewidth * 0.5 - 1) resizingMode:UIImageResizingModeTile];

contentMode属性

  • 带有scale单词的 <图片有可能被拉深>

    • UIViewContentModeScaleToFill
    • 将图片拉伸填充整个imageView
    • 图片显示的尺寸跟imageView的尺寸是一样的
  • 带有aspect单词的:保持图片原来的宽高比
    • UIViewContentModeScaleAspectFit
    • 保证刚好能看到图片的全部
    • UIViewContentModeScaleAspectFill<画图分析>
    • 拉伸至图片的宽度或者高度跟imageView一样
  • 没有带有scale的单词<图片绝对不会被拉伸>

裁剪

//居中显示
imageView.contentMode = UIViewContentModeCenter;
// 裁剪超出imageView边框的部分
imageView.clipsToBounds = YES;

UIImageView-帧动画的基本使用

demo

//创建UIImageView根据图片初始化大小
UIImage *image = [UIImage imageNamed:@"1"];
UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
//让图片居中
images.center = CGPointMake(self.view.bounds.size.width *0.5, self.view.bounds.size.height *0.5);
//把image赋给imageView
images.image =image;
//加到控制器的View中
[self.view addSubview:images];
//创建图片的数组
NSMutableArray *imagesArr = [NSMutableArray array];
//循环添加放入数组
for (int i = 0; i < 20; i++) {
NSString *imageName = [NSString stringWithFormat:@"%d",i+1];
UIImage *ima = [UIImage imageNamed:imageName];
[imagesArr addObject:ima];
}
//让全局变量的imageView赋值
self.imageView = images;
//让全局变量的arr赋值
self.arr = imagesArr;
//imageView透明度
self.imageView.alpha = 0.5;
// 设置播放时长
// 1秒30帧, 一张图片的时间 = 1/30 = 0.03333 20 * 0.0333
self.imageView.animationDuration = 1.0;
// 开始动画
[self.imageView startAnimating];

UIImageView-加载图片的缓存问题

  • imageNamed:

    • 有缓存

      • UIImage *image =[UIImage imageNamed:@"图片名"];
      • 使用场合:图片比较小、使用频率比较高
      • 建议:把需要缓存的图片放到Image.xcassets
    • 没有缓存
      • NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片扩展名"];
      • UIImage *image = [UIImage imageWithContentOfFile:file];
      • 只要方法名带有file的,都是传全路径
      • 使用场合:图片比较大,使用频率比较低
      • 建议:不需要缓存的图片不能放在Assets.xcassets中

总结

1.放在Assets.xcassets中的图片,只能通过文件名访问,没有全路径
2.大批量的图片不要放在Assets.xcassets中,默认就带有缓存
3.放到Assets.xcassets中的图片只能通过图片名去加载,苹果会压缩图片,而且默认带有缓存
4.很多资源都是加载项目中的,项目中的资源都是通过mainBundle来获取的

延迟做一些事情

    //iOS中有很多方式(GCD,dispatch...)
[abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
// 10秒后调用abc的stand:方法,并且传递@“123”参数
// abc可以是任意对象
// NSTimeInterval -->进.h文件分析--->double--->秒

音频文件的简单播放

 // 创建一个音频文件的URL(URL就是文件的路径对象)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音频文件名" withExtention:@“音频文件扩展名”];
//另一写法
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音频文件名.音频文件扩展名" withExtention:nil];
// 创建播放器
self.palyer = [AVPlayer playerWithURL:url];
[self.player play];

Demo

demo1 初始化frame

//方式一 在初始化以后赋值frame
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageNamed:@"2"];
imageView.frame = CGRectMake(10, 10, 100, 200);
imageView.frame = (CGRect){{10,10},{200,200}};
imageView.contentMode = UIViewContentModeScaleAspectFill; //方式二 初始化的时候赋值frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 200)];
UIImage *imgae = [UIImage imageNamed:@"2"];
imageView1.image = imgae; //方式三 根据图片的宽高初始化
UIImage *imgae = [UIImage imageNamed:@"2"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:imgae];
//缺点 移动的时候只能通过center移动
imageView.center = CGPointMake(200, 300); //方式四
UIImage *image = [UIImage imageNamed:@"2"];
UIImageView *imgaeView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, image.size.width, image.size.height)];
imgaeView.image = image;
[self.view addSubview:imgaeView];

demo2 裁剪

// 1.1 创建UIImageView对象
UIImageView *imageView = [[UIImageView alloc]init];
// 1.2 设置frame
imageView.frame = self.view.bounds;
// 1.3 设置背景
imageView.backgroundColor = [UIColor redColor];
// 1.4 设置图片 (png不需要后缀)
imageView.image = [UIImage imageNamed:@"1"];
/** UIViewContentModeRedraw, // 重新绘制 (核心绘图) drawRact //带有Scale,标明图片有可能被拉伸或压缩
UIViewContentModeScaleToFill, // 完全的压缩或拉伸 // Aspect 比例,缩放是带有比例的
UIViewContentModeScaleAspectFit, // 宽高比不变 Fit 适应
UIViewContentModeScaleAspectFill, // 宽高比不变 Fill 填充 //不带有Scale,标明图片不可能被拉伸或压缩
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
// 1.5 设置图片的内容模式
imageView.contentMode = UIViewContentModeScaleAspectFit;
// 2.0 加到控制器的view中
[self.view addSubview:imageView];
// 裁剪多余的部分
imageView.clipsToBounds = YES;

demo3 毛玻璃

// 1.创建UIImageView对象
UIImageView *image = [[UIImageView alloc]init];
// 2. 设置尺寸
//image.frame = CGRectMake(0, 0,self.view.bounds.size.width , self.view.bounds.size.height);
image.frame = self.view.bounds;
NSLog(@"%@",NSStringFromCGRect(image.frame));
// 3. 设置背景颜色
image.backgroundColor = [UIColor yellowColor];
// 4. 设置背景图片
image.image = [UIImage imageNamed:@"1"];
// 5.设置图片的内容模式
image.contentMode = UIViewContentModeScaleAspectFill; // 6.加毛玻璃
// 6.1 创建UIToolBar对象
UIToolbar *tob = [[UIToolbar alloc]init]; // 6.2 设置toolBar的frame
tob.frame = image.frame;
// 6.3 设置毛玻璃的样式
tob.barStyle = UIBarStyleBlack;
tob.alpha = 0.85;
// 6.4 加到imageView中 [self.view addSubview:image];
[self.view addSubview:tob];
原文链接:http://www.jianshu.com/p/4e4801cbda2b

UIImageView 浅析的更多相关文章

  1. UIImageView 的contentMode属性 浅析

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...

  2. 内存管理 & 内存优化技巧 浅析

    内存管理 浅析 下列行为都会增加一个app的内存占用: 1.创建一个OC对象: 2.定义一个变量: 3.调用一个函数或者方法. 如果app占用内存过大,系统可能会强制关闭app,造成闪退现象,影响用户 ...

  3. 浅析SDWebImage

    浅析SDWebImage 在日常的开发过程中,如果去优雅的访问网络的图片并去管理每个工程必须要面对的问题,如果想要在工程里面提供易用.简洁.方便管理的解决方案还是很有挑战的,毕竟还要兼顾图片文件的缓存 ...

  4. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

  5. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  6. 高性能IO模型浅析

    高性能IO模型浅析 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking  ...

  7. netty5 HTTP协议栈浅析与实践

      一.说在前面的话 前段时间,工作上需要做一个针对视频质量的统计分析系统,各端(PC端.移动端和 WEB端)将视频质量数据放在一个 HTTP 请求中上报到服务器,服务器对数据进行解析.分拣后从不同的 ...

  8. AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking

    我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...

  9. Jvm 内存浅析 及 GC个人学习总结

    从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...

随机推荐

  1. 单击dbgrid列标题排序 升降序

    delphi中如何通过单击列标题进行升降排序, 在dbgrid的ontitleclick事件里添加这样的事件处理 procedure TForm3.DBGrid1TitleClick(Column: ...

  2. JavaScript绘制表格并将内容以JSON返回后台

    只是随手记一下 function printTable() { var aText = []; aText.push("<tr"); aText.push("< ...

  3. SpringMVC利用Hibernate validator做字段验证

    1.添加Hiberbate validator相关的jar包 2.字需要验证的formbean 上添加验证的注解,内置注解有: dBean Validation 中内置的 constraint @Nu ...

  4. 基于Flash ActionScript 实现RTMP发布与播放媒本流

    1  为什么要采用Flash ActionScript实现RTMP协议发布或播放媒体流,播放媒体流,协议可控,比如对流媒体数加密,混音等. 2 核心思路使用Flash Socket建立TCP二进制传输 ...

  5. 项目中 添加 swift代码 真机调试 错误

    错误: dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/mobile/Contain ...

  6. angularJS 系列(七)---指令

    ----------------------------------------------------------------------------------- 原文:https://www.s ...

  7. 分析函数 over用法 之row_number() runk_number

    分析函数用于计算基于组的某种聚合值,每个组返回多个行,而聚合函数每个组只返回一个行 表: create table TB_SCORE ( id NUMBER(10), class VARCHAR2(1 ...

  8. Linux下安装MySQL-5.7

    写在前面:此博客是针对MySQL5.7安装教程,其他版本可能略有不同,仅供参考. 第一步:下载mysql 在Linux终端使用wget命令下载网络资源: wget http://mirrors.soh ...

  9. HDU 5828 Rikka with Sequence

    好久没写线段树了,这题作为一个回味.. 第一种操作的话,就是一个延迟标记. 第二种操作可以暴力更新下去,但是有一个优化,如果某区间内所有值都是一样的,或者最大值和最小值相差1,那么到此结束,不要继续往 ...

  10. CodeForces 753C Interactive Bulls and Cows (Hard)

    题意:... 析:随机判断就即可,每次把不正确的删除,经过几次后就基本剩不下了. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...