您的位置:首页 > 产品设计 > UI/UE

UIImageView 浅析

2016-04-28 17:01 323 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: