UIImageView 浅析
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];
UIImageView 浅析的更多相关文章
- UIImageView 的contentMode属性 浅析
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...
- 内存管理 & 内存优化技巧 浅析
内存管理 浅析 下列行为都会增加一个app的内存占用: 1.创建一个OC对象: 2.定义一个变量: 3.调用一个函数或者方法. 如果app占用内存过大,系统可能会强制关闭app,造成闪退现象,影响用户 ...
- 浅析SDWebImage
浅析SDWebImage 在日常的开发过程中,如果去优雅的访问网络的图片并去管理每个工程必须要面对的问题,如果想要在工程里面提供易用.简洁.方便管理的解决方案还是很有挑战的,毕竟还要兼顾图片文件的缓存 ...
- SQL Server on Linux 理由浅析
SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...
- 【深入浅出jQuery】源码浅析--整体架构
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
- 高性能IO模型浅析
高性能IO模型浅析 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking ...
- netty5 HTTP协议栈浅析与实践
一.说在前面的话 前段时间,工作上需要做一个针对视频质量的统计分析系统,各端(PC端.移动端和 WEB端)将视频质量数据放在一个 HTTP 请求中上报到服务器,服务器对数据进行解析.分拣后从不同的 ...
- AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking
我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...
- Jvm 内存浅析 及 GC个人学习总结
从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...
随机推荐
- osgOcean测试
#include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgGA/TrackballManipu ...
- ActiveMQ in Action(4) - Security
关键字: activemq 2.4 Security ActiveMQ支持可插拔的安全机制,用以在不同的provider之间切换.2.4.1 Simple Authentication Plug ...
- Multidimensional Arrays
Overview An array having more than two dimensions is called a multidimensional array in the MATLAB® ...
- Linode开通新加坡机房:vps速度快,价格不变!
vps服务商linode终于开通了新加坡机房中心,这是linode全球第7个机房,满足日益增长的东南亚市场需求.印度.中国.澳大利亚及周边国家都有很好的用户体验. linode新加坡机房采用思科Cis ...
- digitalocean最新优惠码赠送10美元
digitalocean是我非常喜欢的vps服务商,目前手头还有十来个digitalocean vps服务器.用了三年多digitalocean后,我发现digitalocean一点小技巧.比如,如果 ...
- C++ 类中的引用成员变量初始化
刚遇到一个问题,需要的类成员为指针的引用,而引用不能在构造函数里初始化,必须在初始化列表中进行初始化,并且需要该引用在构造函数中的形参必须为引用形式 1: class ThreadParam { 2: ...
- Chapter 15_3 使用环境
创建模块的基本方法的缺点在于,忘记使用local,很容易就污染全局空间. “函数环境”是一种有趣的技术,它能够解决上面的问题.就是让模块的主程序块独占一个环境. 这样不仅它的所有函数可以共享这个tab ...
- javascript注释规范
注释在代码编写过程中的重要性,写代码超过半年的就能深深的体会到.没有注释的代码都不是好代码.为了别人学习,同时为了自己以后对代码进行'升级',看看js/javascript代码注释规范与示例.来自:h ...
- shell脚本调试方法
我们开启了 Shell 脚本调试系列文章,先是解释了不同的调试选项,下面介绍如何启用 Shell 调试模式. 写完脚本后,建议在运行脚本之前先检查脚本中的语法,而不是查看它们的输出以确认它们是否正常工 ...
- WCF部署在IIS上
WCF部署在IIS上 环境vs2010,WCF应用程序.如何将WCF部署在IIS上. 第一步:右键点击项目,选择生成部署包. 第二步:在你项目所在的文件目录下找到Package文件夹,这就是我们的部署 ...