UIImageView的contentMode
来源:互联网 发布:淘宝卖家取消订单投诉 编辑:程序博客网 时间:2024/05/01 22:32
一,写在前面
项目开发中,有时我们需要对UIImageView的contentModel属性做设置,来让图片以不同的方式,显示在UIImageView中. 比如让图片适应,UIImageView; 让UIImageView适应图片来显示等等.
二,了解contentMode
@property(nonatomic) UIViewContentMode contentMode; //default is UIViewContentModeScaleToFill (默认方式,为UIViewContentModeScaleToFill)
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
1.图片小于控件
1)UIViewContentModeScaleToFill
为将图片按照整个区域进行拉伸(会破坏图片的比例)
效果:
2)UIViewContentModeScaleAspectFit
ScaleAspectFit将图片等比例拉伸,可能不会填充满整个区域
效果:
3)UIViewContentModeScaleAspectFill
ScaleAspectFill将图片等比例拉伸,会填充整个区域,但是会有一部分过大而超出整个区域。
效果:
4)UIViewContentModeCenter
效果:
5)UIViewContentModeTop
效果:
6.UIViewContentModeBottom
效果:
7.UIViewContentModeLeft
效果:
8.UIViewContentModeRight
效果:
9.UIViewContentModeTopLeft
效果:
10.UIViewContentModeTopRight
效果:
11.UIViewContentModeBottomLeft
效果:
12.UIViewContentModeBottomRight
效果:
2.图片大于控件
1)UIViewContentModeScaleToFill
效果:
2) UIViewContentModeScaleAspectFit
效果:
3) UIViewContentModeScaleAspectFill,
效果:
4) UIViewContentModeRedraw,
5) UIViewContentModeCenter,
效果:
6)UIViewContentModeTop
效果:
7) UIViewContentModeBottom,
效果:
8)UIViewContentModeLeft
效果:同上
9) UIViewContentModeRight,
效果:同上
10)UIViewContentModeTopLeft,
效果:同上
11)UIViewContentModeTopRight,
效果:同上
12)UIViewContentModeBottomLeft,
效果:同上
13) UIViewContentModeBottomRight,
效果:同上
总结:
1.不管是图片大于控件,还是图片小于控件. 凡是没有带Scale的ContentMode类型,图片本身的大小是不会自动改变来适应UIImageview控件的,改变的仅仅是图片在控件(UIImageView)中的位置.
UIViewContentModeScaleAspectFit 将图片等比例拉伸,可能不会填充满整个区域;
UIViewContentModeScaleAspectFill 将图片等比例拉伸,会填充整个区域,但是会有一部分过大而超出整个区域。
一般在图片适应控件是会选择UIViewContentModeScaleAspectFill 因为它是等比例拉伸,不会破坏图片的比例. 对于超出控件范围的缺陷处理方式,选择用clipsToBounds进行裁剪.
即
testImageView.contentMode =UIViewContentModeScaleAspectFill;
testImageView.clipsToBounds = YES;
3.对于UIViewContentModeRedraw的认识
1>关于contentMode,该模式为视图提供了多种模式用以适用框架矩形。如果对除UIVewContentModeRedraw之外的模式(如UIViewContentModeScaleToFill)都不满足需求,或者说有特定需要自定义绘制视图,可以设置为此值。那么将在视图适应框架矩形变化时缺省自动调用setNeedsDispllay或setNeedsDisplayInRect:,从而重绘视图
2>而向视图发送setNeedsDisplay(或setNeedsDisplayInRect:)消息时,无论此时contentMode为何种模式,都将强制调用drawRect:
三,关于UIImageView的一些属性方法
UIImageView *imageView1 = [[UIImageView alloc] init];UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)];UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)];UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];
第二种:
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpeg"];
UIImage *images=[UIImage imageWithContentsOfFile:filePath];
第三种:
NSData *data=[NSData dataWithContentsOfFile:filePath];
UIImage *image2=[UIImage imageWithData:data];
3>关于UIImageView 两种加载方式
1)用imageNamed的方式加载时,系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的内存是一件相对来说比较 麻烦的事情。
2)利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。
UIImage *image = [[UIImage alloc]initWithContentsOfURL:(NSURL *)];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:(NSString *)];
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle); <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
这个方法的参数angle的单位是弧度,而不是我们最常用的度数,所以可以写一个宏定义:
#define degreesToRadians(x) (M_PI*(x)/180.0) 用于将度数转化成弧度。
imageView.alpha = (CGFloat) al; // 设置透明度
imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置高亮时显示的图片
imageView.image = (UIImage *)image; // 设置正常显示的图片
[imageView sizeToFit]; // 将图片尺寸调整为与内容图片相同
imageView.animationImages = 存储UIImage对象的数组; // 设定所有的图片在多少秒内播放完毕 imageView.animationDuration = [对象数组 count]; // 不重复播放多少遍,0表示无数遍 imageView.animationRepeatCount = 0; // 开始播放 [imageView startAnimating];
imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap];注意:
1>圆角
imageView.layer.masksToBounds = YES; imageView.layer.cornerRadius = 10;2>边框颜色和大小
imageView.layer.borderColor = [UIColor orangeColor].CGColor; imageView.layer.borderWidth = 2;
//是否栅格化。 imageView.layer.ShouldRasterize = NO // YES:会栅格化层中的数据(如:图像) NO:不栅格化 . 我们知道,栅格化数据是将矢量图变为位图。所以,如果层中的内容是 变动的,每次都需要栅格化,就会影像效率。一般设置为NO // 设置投影偏移量,CGSizeMake(x轴方向, y轴方向) imageView.layer.shadowOffset=CGSizeMake(1, 1); // 设置投影颜色 imageView.layer.shadowColor =[UIColor redColor].CGColor; // 设置投影半径 imageView.layer.shadowRadius=3; // 设置透明度 imageView.layer.shadowOpacity =1;
四,UIImageView对图片的操作方法
- (UIImage *)rescaleImageToSize:(CGSize)size { CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height); UIGraphicsBeginImageContext(rect.size); [self drawInRect:rect]; // scales image to rect UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resImage; }
- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改变大小
+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图片
+ (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; //保存图片到媒体库
- UIImageview的contentMode属性
- UIImageView 的contentMode属性
- UIImageView的contentMode属性
- UIImageView 的contentMode属性
- UIImageview的contentmode介绍
- UIImageView 的contentMode属性
- UIImageView 的contentMode属性
- UIImageView的ContentMode属性
- UIImageView 的contentMode属性
- UIImageView的contentMode
- UIImageView 的contentMode属性
- UIImageView 的contentMode属性
- UIImageView 的contentMode属性应用
- UIImageView 的contentMode属性应用
- UIImageView 的contentMode属性应用
- UIImageView 的contentMode属性应用
- UIImageView 的contentMode属性应用
- UIImageView的contentMode属性详解
- 几款好用的比较工具
- 背景建模--内核密度估计法
- linux内核研究(一)
- qt序列化 (二)
- java中正则表达式的使用(2)——列出百度搜索的前十项
- UIImageView的contentMode
- Wet Shark and Odd and Even
- Java中的接口
- iOS开发之遍历Model类的属性并赋值
- iOS AFNetworking 简单使用
- 导航栏透明渐变(iOS)
- jsp页面直接跳转编码方式
- qt序列化 (三)
- 操作字符串