UIImageView摘录(持续更新)

来源:互联网 发布:淘宝的电子商务系统 编辑:程序博客网 时间:2024/06/05 12:49

UIImgeView知识零散摘取:

第一部分:

>>1//图片的响应用户默认是关闭的,要自己开启

 self.img.userInteractionEnabled = YES;

>>2.也可以设置背景图

 self.img.backgroundColor = [UIColor lightGrayColor]; 

 self.img.backgroundColor = [UIColor colorWithPatterImages:[UIImage imageName:@""]];

>>3//初始化

 UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)]; //需要设置图片 UIImage 

 第一种:[imageView setImage:[UIImage imageNamed:@"1.jpeg"]]; 

 第二种: NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpeg"]; 

 UIImage *images=[UIImage imageWithContentsOfFile:filePath]; //[imageView setImage:images]; //

第三种: NSData *data=[NSData dataWithContentsOfFile:filePath]; 

 UIImage *image2=[UIImage imageWithData:data];

 [imageView setImage:image2]

其中第一 二种属于一种,共两种:

 1)用imageNamed的方式加载时,系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的内存是一件相对来说比较麻烦的事情。例如:如果利用imageNamed的方式加载图像到一个动态数组NSMutableArray,然后将将数组赋予一个UIView的对象的animationImages进行逐帧动画,那么这将会很有可能造成内存泄露。并且释放图像所占据的内存也不会那么简单。但是利用imageNamed加载图像也有自己的优势。对于同一个图像系统只会把它Cache到内存一次,这对于图像的重复利用是非常有优势的。例如:你需要在一个TableView里重复加载同样一个图标,那么用imageNamed加载图像,系统会把那个图标Cache到内存,在Table里每次利用那个图像的时候,只会把图片指针指向同一块内存。这种情况使用imageNamed加载图像就会变得非常有效。

 2)利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。 无论用哪种方式加载图像,图像使用结束后,一定要记得显示释放内存。

 在项目汤姆猫中

 // 加载图片 

 11.不释放内存,会累加内存,因为有缓存 

 // imageNamed: 有缓存(传入文件名) //

 UIImage *image = [UIImage imageNamed:filename]; 

 22.用完就释放掉

 // imageWithContentsOfFile: 没有缓存(传入文件的全路径)

 NSBundle *bundle = [NSBundle mainBundle]; 

 NSString *path = [bundle pathForResource:filename ofType:nil];

 UIImage *image = [UIImage imageWithContentsOfFile:path]; 

 ??? 回去试一下22.和第三种方法看看内存大小,和内存释放是否???????

>>4 设置边框样式

//—————— 设置边框 —————— /设置layer

 CALayer *layer=[backView layer]; //是否设置边框以及是否可见

 [layer setMasksToBounds:YES]; //设置边框圆角的弧度 

 [layer setCornerRadius:10.0]; //设置边框线的宽 //

 [layer setBorderWidth:1]; //设置边框线的颜色

 [layer setBorderColor:[[UIColor blackColor] CGColor]]; //给iamgeview添加阴影 和边框

 UIImageView * imgvPhoto = [UIImageView alloc] init]; //添加边框

 CALayer * layer = [_imgvPhoto layer];

 layer.borderColor = [ [UIColor whiteColor] CGColor]; 

 layer.borderWidth = 5.0f; //添加四个边阴影 

 _imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; _

imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0); 

 _imgvPhoto.layer.shadowOpacity = 0.5; _

imgvPhoto.layer.shadowRadius = 10.0;给iamgeview添加阴影 < wbr > 和边框 //添加两个边阴影

 _imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; 

 _imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4); _

imgvPhoto.layer.shadowOpacity = 0.5; _

imgvPhoto.layer.shadowRadius = 2.0; 

>>5 view的显示内容模式UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,

有以下几个常量可供设定:

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

UIViewContentModeRedraw

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。

UIViewContentModeScaleToFill属性会导致图片变形。

UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。

UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

关于UIView的autoresizingMask属性的研究在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高。

enum { 

 UIViewAutoresizingNone = 0, 

 UIViewAutoresizingFlexibleLeftMargin = 1 << 0,

 UIViewAutoresizingFlexibleWidth = 1 << 1, 

 UIViewAutoresizingFlexibleRightMargin = 1 << 2, 

 UIViewAutoresizingFlexibleTopMargin = 1 << 3, 

 UIViewAutoresizingFlexibleHeight = 1 << 4,

 UIViewAutoresizingFlexibleBottomMargin = 1 << 5}; 

UIViewAutoresizingNone就是不自动调整。UIViewAutoresizingFlexibleLeftMargin 自动调整与superView左边的距离,保证与superView右边的距离不变。UIViewAutoresizingFlexibleRightMargin 自动调整与superView的右边距离,保证与superView左边的距离不变。UIViewAutoresizingFlexibleTopMargin 自动调整与superView顶部的距离,保证与superView底部的距离不变。UIViewAutoresizingFlexibleBottomMargin 自动调整与superView底部的距离,也就是说,与superView顶部的距离不变。UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与superView左边和右边的距离不变。UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与superView顶部和底部的距离不变。UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin 自动调整与superView左边的距离,保证与左边的距离和右边的距离和原来距左边和右边的距离的比例不变。比如原来距离为20,30,调整后的距离应为68,102,即68/20=102/30。其它的组合类似。 imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth;

>>6、为UIImageView添加单击事件:

 imageView.userInteractionEnabled = YES;(这里是默认值为NO)

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];

[imageView addGestureRecognizer:singleTap];

0 0