zoukankan      html  css  js  c++  java
  • iOS 中 为UIView添加背景图片

    创建UIImage的方法有两种:

        UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存

        

        NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];

       UIImage *image1 = [UIImageimageWithContentsOfFile:path];//这种会释放内存


    那么,为UIView添加背景图片可以有三种方法:

    1.在UIView上添加一个UIImageView

        UIImageView *imageView = [[UIImageViewalloc]initWithFrame:self.view.bounds];

        imageView.image = [[UIImageimageNamed:@"image.jpg"]stretchableImageWithLeftCapWidth:10topCapHeight:10];

        [self.viewaddSubview:imageView];

       //这种方式,如果原始图片不小不够,则会拉伸以满足View的尺寸,在View释放之后没有内存保留。


    2.将图片作为UIView的背景色

        //1.imageNamed方式

        self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"image.jpg"]];

        //2.方式

        NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];

        self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageWithContentsOfFile:path]];

        

       //这两种方式都会在生成color时占用大量的内存。如果图片大小不够,就会平铺多张图片,不会去拉伸图片以适应View的大小。

       //在View释放后,1中的color不会跟着释放,而是一直存在内存中;2中的color会跟着释放掉,当然再次生成color时就会再次申请内存。

    3.其他方式(推荐)

        NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];

        

       UIImage *image = [UIImageimageWithContentsOfFile:path];

        self.view.layer.contents = (id)image.CGImage;



  • 相关阅读:
    修复PLSQL Developer 与 Office 2010的集成导出Excel 功能
    Using svn in CLI with Batch
    mysql 备份数据库 mysqldump
    Red Hat 5.8 CentOS 6.5 共用 输入法
    HP 4411s Install Red Hat Enterprise Linux 5.8) Wireless Driver
    变更RHEL(Red Hat Enterprise Linux 5.8)更新源使之自动更新
    RedHat 5.6 问题简记
    Weblogic 9.2和10.3 改密码 一站完成
    ExtJS Tab里放Grid高度自适应问题,官方Perfect方案。
    文件和目录之utime函数
  • 原文地址:https://www.cnblogs.com/wangluochong/p/5555234.html
Copyright © 2011-2022 走看看