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

UIImageView用法大全 - iOS - UI基础知识总结7

2015-06-06 16:56 567 查看
在RootViewController.h中创建一个imageView属性(记得释放)

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@property (nonatomic, retain) UIImageView *imageView;

@end

在RootViewController.m中实现具体操作

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];

// UIImageView,顾名思义,是用来放置图片的

// 一、创建UIImageView对象有五种方法_______________________________

/*

self.imageView = [[UIImageView alloc]init];

self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];

self.imageView = [[UIImageView alloc]initWithImage:(UIImage *)];

self.imageView = [UIImageView alloc]initWithImage:<#(UIImage *)#> highlightedImage:<#(UIImage *)#>];

self.imageView = [[UIImageView alloc]initWithCoder:<#(NSCoder *)#>];

比较常用的是前边三个,至于第四个,当这个ImageView的highlighted属性是YES时,显示的就是参数highlightedImage,一般情况下显示的是第一个参数UIImage;第五个用法稍微复杂一点

*/

// 二、创建UIImage对象有三种方法_______________________________

/*

UIImage *image = [UIImage imageNamed:<#(NSString *)#>];

UIImage *image = [UIImage imageWithContentsOfFile:<#(NSString *)#>];

UIImage *image = [UIImage imageWithData:<#(NSData *)#>];

imageNamed: 如果它存在的话,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象。如果缓存中没有找到相应的图片,这个方法从指定的文档中加载然后缓存并返回这个对象。因此imageNamed的优点是当加载时会缓存图片。所以当图片会频繁的使用时,那么用imageNamed的方法会比较好。例如:你需要 在一个TableView里的TableViewCell里都加载同样一个图标,那么用imageNamed加载图像效率很高。系统会把那个图标Cache到内存,在TableViewCell里每次利用那个图像的时候,只会把图片指针指向同一块内存。正是因此使用imageNamed会缓存图片,即将图片的数据放在内存中,iOS的内存非常珍贵并且在内存消耗过大时,会强制释放内存,即会遇到memory
warnings。而在iOS系统里面释放图像的内存是一件比较麻烦的事情,有可能会造成内存泄漏。例如:当一个UIView对象的animationImages是一个装有UIImage对象动态数组NSMutableArray,并进行逐帧动画。当使用imageNamed的方式加载图像到一个动态数组NSMutableArray,这将会很有可能造成内存泄露。原因很显然的。

imageWithContentsOfFile:仅加载图片,图像数据不会缓存。因此对于较大的图片以及使用情况较少时,那就可以用该方法,降低内存消耗。

利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,尽量使用imageWithData的方式加载图像。

*/

// 初始化UIImageView

self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4, self.view.frame.size.height/4, self.view.frame.size.width/2, self.view.frame.size.height/2)];

// 初始化UIImage

// 1、imageNamed方式加载

UIImage *image = [UIImage imageNamed:@"image"];// png格式的图片可以不写格式,只写名字就能被系统找到

// 2、imageWithContentsOfFile方式加载

// NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"image" ofType:@"png"];

// UIImage *image = [UIImage imageWithContentsOfFile:fielPath];

// 3、dataWithContentsOfFile方式加载

// UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fielPath]];

// 4、dataWithContentsOfURL方式加载

// UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://desktop.kole8.com/handset_desktop/desk_file-11/17/37/2014/12/2014121211265447.jpg"]]];

// 将image添加到imageView上

self.imageView.image = image;

// 将imageView添加到self.view上

[self.view addSubview:self.imageView];

// 释放

[self.imageView release];

// 三、UIImageView的常用设置_______________________________

// 1、设置imageView的半径

self.imageView.layer.masksToBounds = YES;// 经常与cornerRadius属性使用。当设置为YES时,圆角外的区域将被遮盖

self.imageView.layer.cornerRadius = 10;

// 2、设置imageView的透明度

self.imageView.alpha = 1.0;

// 3、隐藏图片(系统默认是NO)

self.imageView.hidden = NO;

// 4、删除image(将内容置空)

// self.imageView.image = nil;

// 5、两种适应所给图片的方法的区别:

// [self.imageView sizeToFit];

// calls sizeThatFits: with current view bounds and changes bounds size

// 利用当前视图的边界尺寸调用sizeThatFits:方法并且改变边界的尺寸

// [self.imageView sizeThatFits:CGSizeMake(200, 500)];

// return 'best' size to fit given size. does not actually resize view. Default is return existing view size

// 返回一个适应所给尺寸的最好的尺寸,实际上不会改变view的尺寸,默认返回现存的view尺寸

// 6、是否栅格化。

// YES:会栅格化层中的数据(如:图像)

// NO:不栅格化

// 栅格化数据是将矢量图变为位图。所以,如果层中的内容是变动的,每次都需要栅格化,就会影像效率。一般设置为NO

[self.imageView.layer setShouldRasterize:NO];

self.imageView.userInteractionEnabled = YES;

// 7、设置边框宽度

[self.imageView.layer setBorderWidth:3.0];

// 8、设置边框颜色

[self.imageView.layer setBorderColor:[[UIColor orangeColor]CGColor]];

[self.imageView.layer setShadowOpacity:1.0];

// 9、设置投影偏移量,CGSizeMake(x轴方向,y轴方向)

[self.imageView.layer setShadowOffset:CGSizeMake(15, 15)];

// 10、设置投影颜色

[self.imageView.layer setShadowColor:[[UIColor blackColor]CGColor]];

// 11、设置投影半径

[self.imageView.layer setShadowRadius:2.0];

// 12、设置投影的不透明度(必须设置,否则没有阴影)

self.imageView.layer.shadowOpacity = 0.8;

// 四、通过设置imageView的bounds属性间接对突变进行缩放_______________________________

// 缩放后图片中心和imageView中心一致

// self.imageView.bounds = CGRectMake(0, 0, 300, 400);

// self.imageView.bounds = CGRectMake(0, 0, 100, 180);

// 五、UIImageView的contentMode属性_______________________________

/*

UIViewContentModeScaleToFill, 图片调整自身比例充满imageView,可能会造成图片变形

UIViewContentModeScaleAspectFit, 图片保持自身比例尽量充满imageView,可能会出现上下或左右是空白的(只充满上下或左右边框)

UIViewContentModeScaleAspectFill, 图片以自身的中心点为基准保持自身原始比例充满到整个imageView,可能会出现部分图片无法显示(功能类似于UIViewContentModeCenter)

UIViewContentModeRedraw, 图片重绘,改变自身以充满imageView,可能会造成图片变形

UIViewContentModeCenter, 图片中心和imageView中心对齐,保持自身原始比例充满整个imageView,可能会造成图片四周某些部分无法展示

UIViewContentModeTop, 从图片头部开始往下展示,充满整个imageView,可能会造成图片下面和左右部分 有些无法展示

UIViewContentModeBottom, 从图片底部开始往下展示,充满整个imageView,可能会造成图片上面和左右部分 有些无法展示

UIViewContentModeLeft, 从图片左边开始往右展示,充满整个imageView,可能会造成图片上下部分和右部分 有些无法展示

UIViewContentModeRight,

UIViewContentModeTopLeft, 从图片左上角开始展示,充满整个imageView

UIViewContentModeTopRight,

UIViewContentModeBottomLeft,

UIViewContentModeBottomRight,

*/

// self.imageView.contentMode = UIViewContentModeScaleAspectFit;

// 六、UIImageView的transform属性_______________________________

// 1、CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>) 其中tx与ty表示想要往x或者y方向移动多少,而不是移动到哪个位置

// self.imageView.transform = CGAffineTransformMakeTranslation(10, 100);

// 2、CGAffineTransformMakeRotation(<#CGFloat angle#>) 要注意它是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。这个方法的参数angle的单位是弧度,而不是我们最常用的度数(一个圆是360度,2π弧度) 把弧度化成度的公式:度=弧度×180°/π

// self.imageView.transform = CGAffineTransformMakeRotation(0.1);

// 3、CGAffineTransformMakeScale(<#CGFloat sx#>, <#CGFloat sy#>) 其中,CGFloat sx与CGFloat sy分别表示将原来的宽度和高度缩放到多少倍

// self.imageView.transform = CGAffineTransformMakeScale(0.5, 1.5);

// 七、播放一系列图片_______________________________

// 先准备一个imageView用来存放要不放的图片(假设装入10张图片)

// NSMutableArray *imageArray = [NSMutableArray array];

// for (int i = 0; i < 10; i++) {

// UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d", i+1]];

// [imageArray addObject:image];

// }

// // 将装有图片的数组设置为动画图片数组

// self.imageView.animationImages = imageArray;

// // 设置播放时间(多长时间播放完)

// self.imageView.animationDuration = 10;

// // 设置循环播放的次数(设为0时为无限播放,系统默认是0)

// self.imageView.animationRepeatCount = 1;

// // 开始不放动画

// [self.imageView startAnimating];

// // 结束播放

// [self.imageView stopAnimating];

// 八、为图片添加点击事件_______________________________

// 先打开用户交互

self.imageView.userInteractionEnabled = YES;

// 再创建手势

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];

// 将手势添加到图片上

[self.imageView addGestureRecognizer:tapGesture];

// 释放内存

[tapGesture release];

// 九、展现网络图片的三种方法的区别_______________________________

// 1、最简单的做法:通过图片地址直接加载

// UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://e.hiphotos.baidu.com/image/w%3D310/sign=f569c6dd8813632715edc432a18ea056/d52a2834349b033bbf03a6db17ce36d3d539bd2c.jpg"]]];

// self.imageView.image = image1;

// 这么做,最直接的问题就是阻塞UI线程了,于是考虑利用NSOperationQueue来异步加载图片

// 2、用NSOperationQueue来异步加载图片

// NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];

// NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:nil];

// [operationQueue addOperation:op];

// 这么做的话,就可以避免阻塞UI线程了。当图片异步加载完成后,就会展现出来。但是,第二次进入该界面,还是要重新下载图片,用户体验不好,且浪费资源(比如耗电)。于是,考虑缓存已经下载的图片。模仿操作系统(Cache - Memory - Disk),缓存图片也可以采取两层模型:内存和磁盘。保存到内存中比较简单,只需要用NSDictionary来维护即可。而保存到磁盘,涉及到本地文件读写,可以参考“文件和数据管理”。

}

#pragma mark - tapGestureAction

- (void)tapGestureAction:(UITapGestureRecognizer *)sender

{

// 随机缩放图片大小

self.imageView.transform = CGAffineTransformMakeScale(arc4random()%100/50.0 + 0.1, arc4random()%100/50.0 + 0.1);

}

#pragma mark - downloadImage

- (void)downloadImage:(NSInvocationOperation *)sender

{

NSURL *imageURL = [NSURL URLWithString:@"http://e.hiphotos.baidu.com/image/w%3D310/sign=f569c6dd8813632715edc432a18ea056/d52a2834349b033bbf03a6db17ce36d3d539bd2c.jpg"];

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

self.imageView.image = image;

}

- (void)dealloc

{

[_imageView release];

[super dealloc];

}

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