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)中的位置.

2.不管是图片大于控件,还是图片小于控件.
UIViewContentModeScaleToFill 为将图片按照整个区域进行拉伸(会破坏图片的比例);

UIViewContentModeScaleAspectFit 将图片等比例拉伸,可能不会填充满整个区域; 

UIViewContentModeScaleAspectFill 将图片等比例拉伸,会填充整个区域,但是会有一部分过大而超出整个区域。

一般在图片适应控件是会选择UIViewContentModeScaleAspectFill 因为它是等比例拉伸,不会破坏图片的比例. 对于超出控件范围的缺陷处理方式,选择用clipsToBounds进行裁剪.

    testImageView.contentMode =UIViewContentModeScaleAspectFill;
    testImageView.clipsToBounds = YES;

3.对于UIViewContentModeRedraw的认识

 1>关于contentMode,该模式为视图提供了多种模式用以适用框架矩形。如果对除UIVewContentModeRedraw之外的模式(如UIViewContentModeScaleToFill)都不满足需求,或者说有特定需要自定义绘制视图,可以设置为此值。那么将在视图适应框架矩形变化时缺省自动调用setNeedsDispllay或setNeedsDisplayInRect:,从而重绘视图
 2>而向视图发送setNeedsDisplay(或setNeedsDisplayInRect:)消息时,无论此时contentMode为何种模式,都将强制调用drawRect:

三,关于UIImageView的一些属性方法

1.初始化
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 *)];
注释:
   1>第一,二,三种比较常用:其中第一种在用masonry布局时常用.第四种,当这个ImageView的highlighted属性是YES时,显示的就是参数highlightedImage,一般情况下显示的是第一个参数UIImage
   2>关于UIImage
       第一种:
       UIImage * images = [imageView setImage:[UIImage imageNamed:@"1.jpeg"]];
       第二种:
       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到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的内存是一件相对来说比较  麻烦的事情。
            例如:如果利用imageNamed的方式加载图像到一个动态数组NSMutableArray,然后将将数组赋予一个UIView的对象的animationImages进行逐帧动画,那么这将会很有可能造成内存泄露。并且释放图像所占据的内存也不会那么简单。但是利用imageNamed加载图像也有自己的优势。对于同一个图像系统只会把它Cache到内存一次,这对于图像的重复利用是非常有优势的。
例如:你需要在一个TableView里重复加载同样一个图标,那么用imageNamed加载图像,系统会把那个图标Cache到内存,在Table里每次利用那个图像的时候,只会把图片指针指向同一块内存。这种情况使用imageNamed加载图像就会变得非常有效。
       2)利用NSData方式加载时,图像会被系统以数据方式加载到程序。当你不需要重用该图像,或者你需要将图像以数据方式存储到数据库,又或者你要通过网络下载一个很大的图像时,请尽量使用imageWithData的方式加载图像。
       3)  如果找到图片,装载到iPhone系统缓存图象。那意味图片是(理论上)放在内存里作为cache的。因此如果图片资源多了或大了,此方式容易引起发生内存警告从而导致自动退出的问题。 最好是通过直接读取文件路径[UIImage imageWithContentsOfFile]解决掉这个问题.
           UIImage *image = [[UIImage alloc]initWithContentsOfURL:(NSURL *)];
           UIImage *image = [[UIImage alloc]initWithContentsOfFile:(NSString *)];
2.transform属性
      1>移动   CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
                    imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
              注意:
                    其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少
      2>旋转   CGAffineTransformMakeRotation(CGFloat angle);
         imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);      <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">    </span>
             注意:
                    它是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。
                    这个方法的参数angle的单位是弧度,而不是我们最常用的度数,所以可以写一个宏定义:
                    #define degreesToRadians(x) (M_PI*(x)/180.0)   用于将度数转化成弧度。
   
      3>缩放   CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h)
             注意:
                   CGFloat scale_w与CGFloat scale_h分别表示将原来的宽度和高度缩放到多少倍,下图是缩放到原来的0.6倍的示意图:
   其它的属性:
        imageView.hidden = YES或者NO;    // 隐藏或者显示图片
        imageView.alpha = (CGFloat) al;    // 设置透明度
        imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置高亮时显示的图片
        imageView.image = (UIImage *)image; // 设置正常显示的图片
        [imageView sizeToFit];    // 将图片尺寸调整为与内容图片相同
3.播放一系列图片
       imageView.animationImages = 存储UIImage对象的数组;         // 设定所有的图片在多少秒内播放完毕       imageView.animationDuration = [对象数组 count];        // 不重复播放多少遍,0表示无数遍       imageView.animationRepeatCount = 0;       // 开始播放       [imageView startAnimating];
4.单击事件
     由于UIImageView不是继承于UIControl,而是继承于UIView,所以它的触发事件,可以通过手势触发
     imageView.userInteractionEnabled = YES;     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];     [imageView addGestureRecognizer:singleTap];
     注意:
             UIImageView的userInteractionEnabled 默认状态下为NO.
5.设置圆角,边框颜色和大小,及其它设置
    1>圆角
    imageView.layer.masksToBounds = YES;    imageView.layer.cornerRadius = 10;
    2>边框颜色和大小
    imageView.layer.borderColor = [UIColor orangeColor].CGColor;    imageView.layer.borderWidth = 2;
   3>其它的一些设置    
    //是否栅格化。    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对图片的操作方法

    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;        }
    2.UIImageView的自带的常用方法
       - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;   //图片缩放裁剪
       - (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;  //保存图片到媒体库

0 0
原创粉丝点击