美文网首页iOS的心路里程
iOS控件 - - - UIImageView - - 图像控件

iOS控件 - - - UIImageView - - 图像控件

作者: 罂粟之城 | 来源:发表于2017-12-14 19:58 被阅读159次

    UIImageView控件是一种图像控件,是可以作为加载和展示图像的控件。

    UIImageView基本使用
    初始化控件:       UIImageView *iv = [[UIImageView alloc]init];
    设置图片:        iv.image=[UIImage imageNamed:@"图片名.png/.jpg"];
    设置控件属性:     iv.layer.masksToBounds=YES;
                     iv.layer.cornerRadius=(width=height)/2.0f;
    //设置保持图片位于frame的中间,使得图片不会被拉伸和压缩
                    iv.contentMode = UIViewContentModeCenter; 
    
    UIImageView控件除了和其它控件同样的操作之外,其他需要注意:
    1. 加载图片使用最基本的“imageNamed:”方法,这一般使用于控件的背景图加载,如UIButton也有backgroundImage的设置,同样使用imageNamed:方法加载
    2. 不过一些,如需要使用大量图片加载时,一般使用一些库去加载需要展示的图片,如SDWebImage(github:SDWebImage),FlyImage(github:FlyImage)。

    UIImage -- imageNamed 和 imageWithContentsOfFile 区分

    在 iOS 应用中加载图片通常有 - [UIImage imageNamed:] 和 -[UIImage imageWithContentsOfFile:] 两种方式。它们的不同在于前者会对图片进行缓存,而后者只是简单的从文件加载文件。
    UIImage *img = [UIImage imageNamed:@"myImage"]; // caching
    // or
    UIImage *img = [UIImage imageWithContentsOfFile:@"myImage"]; // no caching
    在整个程序运行的过程中,当你需要加载一张较大的图片,并且只会使用它一次,那么你就没必要缓存这个图片,这时你可以使用 -[UIImage imageWithContentsOfFile:],这样系统也不会浪费内存来做缓存了。
    当然,如果你会多次使用到一张图时,用 - [UIImage imageNamed:] 就会高效很多,因为这样就不用每次都从硬盘上加载图片了。
    

    如何判断UIImageView为空

        UIImage *image = [UIImage imageNamed:@""];
        CGImageRef cgref = [image CGImage];
        CIImage *cim = [image CIImage];
    //
        if (cim == nil && cgref == NULL)
        {
            NSLog(@"no image");
        } else {
            NSLog(@"imageView has a image");
        }
    
    UIIImageView控件的内容不多,基本的使用,不以偏概全,只授之以渔,有更好的操作也会及时更新如果您有UIImageView控件的更好使用欢迎留言交流!

    相关文章

      网友评论

        本文标题:iOS控件 - - - UIImageView - - 图像控件

        本文链接:https://www.haomeiwen.com/subject/jcklwxtx.html