iOS UIImageView(contentMode)图片的三种填充方式

iOS UIImageView(contentMode)图片的三种填充方式,第1张

一:规律:

   1> 但凡取值中包含Scale单词的,都会对图片进行拉伸(缩放)

   2> 但凡取值中没有出现Scale单词的,都不会对图片进行拉伸

       3>   但凡取值中包含Aspect单词的, 都会按照图片的宽高比来拉伸.

二:填充模式:     

     1.UIViewContentModeScaleToFill,

     > 会按照UIImageView的宽高比来拉伸图片

     > 直到让整个图片都填充UIImageView为止

     > 因为是按照UIImageView的宽高比来拉伸,所以图片会变形

     2.UIViewContentModeScaleAspectFit,常常配合:clipsToBounds(将超出的图片部分裁剪掉)

     > 会按照图片的宽高比来拉伸

     > 要求整张图片都必须在UIImageView的范围内

     > 并且宽度和高度其中一个必须和UIImageView一样

     > 居中显示

     3.UIViewContentModeScaleAspectFill,

     > 会按照图片的宽高比来拉伸

     > 要求整张图片必须填充UIImageView

     > 并且图片的宽度或者高度其中一个必须和UIImageView一样

> 因为是按照图片的宽高比来拉伸, 所以图片不会变形  

1.设置一般View的背景

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imgName.png"]]

imgView.frame = self.view.bounds

imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth

[self.view insertSubview:imgView atIndex:0]

2.设置View的背景颜色,使用图片,效果和设置背景图片比较类似

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imgName.png"]]]

3.设置UITableView的背景

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imgName.png"]]

imgView.frame = self.view.bounds

imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth

[self.tableView setBackgroundView:imgView]

4.设置UITableView的cell颜色

//方法一:

cell.contentView.backgroundColor = [UIColor redColor]

//方法二:

UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

UIView bgview = [[UIView alloc]initWithFrame:CGRectMake(0,0,1,1)]

bgview.opaque = YES

bgview.backgroundColor = [UIColor orangeColor]

[cell setBackgroundView:bgview]

//方法三:

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImage"]] 

    创建并设置默认图, 也可以

    UIImageView*imageView = [[UIImageView alloc] init]

    imageView.image= [UIImageimageNamed:@"bgImage"]

    还可以这样先设置imageview的大, 在设置图片

     UIImageView*imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0,144,SCREEN_Width,50))]

    imageView.image= [UIImageimageNamed:@"bgImage"]

    由此可看imageview的frame可以这样设置

     imageView.frame=CGRectMake(0,144,SCREEN_Width,50)

    通常我们使用的的imageview都会添加圆角边框

     imageView.layer.masksToBounds = YES

    imageView.layer.cornerRadius=25

    imageView.layer.borderColor = [UIColor blueColor].CGColor

    imageView.layer.borderWidth=1

    这个圆角和边框像view和label以及button的设置方式都是一样的 当然imageview也一样

     imageView.backgroundColor= [UIColorclearColor]图片设置背景颜色, 我通常使用clearColor  透明

     imageView.userInteractionEnabled = YES图片设置成可交互, 设置为NO则不能交互

     [self.viewaddSubview: imageView]添加视图也可叫做显示视图

    设置图片内容的布局方式 imageView.contentMode

    这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等

    imageView.contentMode = UIViewContentModeScaleAspectFit

     UIViewContentMode contentMode枚举类型

        (1)  UIViewContentModeScaleToFill    默认,对图片进行拉伸处理(不是按比例),是充满bouns

        (2)  UIViewContentModeScaleAspectFit    按原图比例进行拉伸,是图片完全展示在bouns中

        (3)  UIViewContentModeScaleAspectFill    按原图比例填充,使图片展示在bouns中,可能只显示部分

        (4)  UIViewContentModeRedraw    重划边界变化(重设 - setNeedsDisplay)

        (5)  UIViewContentModeCenter    图片显示在imageview的正中间,原图大小

        (6)  UIViewContentModeTop    图片显示在imageview的上部,原图大小

        (7)  UIViewContentModeBottom    图片显示在imageview的下部,原图大小

        (8)  UIViewContentModeLeft    图片显示在imageview的左部,原图大小

        (9)  UIViewContentModeRight    图片显示在imageview的右部,原图大小

        (10)  UIViewContentModeTopLeft    图片显示在imageview的左上部,原图大小

        (11)  UIViewContentModeTopRight    图片显示在imageview的右上部,原图大小

        (12)  UIViewContentModeBottomLeft    图片显示在imageview的左下部,原图大小

        (13)  UIViewContentModeBottomRight    图片显示在imageview的右下部,原图大小

     imageView.alpha = 1.0   设置图片透明度

        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"]

        NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"]

        NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"]

        imageView.animationImages = @[[UIImage imageWithContentsOfFile:path1],[UIImage imageWithContentsOfFile:path2],[UIImage imageWithContentsOfFile:path3]]

        imageView.animationDuration = 5.0f   设置循环一次的时间

        imageView.animationRepeatCount = 0    // 设置循环次数(0为无线循环)

        [imageView startAnimating]            // 开始动画

        [imageView stopAnimating]              // 停止动画

    NSData *imageData = [NSData dataWithContentsOfFile:path]

    UIImage *image4 = [UIImage imageWithData:imageData]

    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"]

    UIImage *image2 = [UIImage imageWithContentsOfFile:path]

    ImageView.hidden = NO    隐藏或者显示图片 YES为隐藏

    [ImageView sizeToFit]    将图片尺寸调整为与内容图片相同

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)] // 设置手势

  [ImageView addGestureRecognizer:singleTap] // 给图片添加手势


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11666783.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存