iOS 中背景图片的设置

来源:互联网 发布:海报工厂软件 编辑:程序博客网 时间:2024/05/16 09:21


一般我们设置背景图片有两种方法:

//不释放内存,比较占内存,有缓存,加载常用的图片UIImage *image = [UIImage imageNamed:FileName];//会释放内存,没有缓存  比较适合加载大的图片UIImage *image = [UIImage imageWithContentsOfFile:path];

在 view 上添加背景图片的三种方式,推荐最后一种:


1、通过加一个 UIImageView添加到UIView(可以使用)

UIImageView *imageview = [UIImageView alloc]initWithFrame:view.bounds];imageview.image = [[UIImage imageNamed@"name.png"] stretchableImageWithLeftCapWith:left topCapHeight:top];[view addSubview: imageView];

2、通过图片来生成 UIColor设置ViewbackgroundColor (不推荐)

a、imageNamed方式: view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"name.png"]];b、contentOfFile方式:NSString *path = [NSBundle mainBundle] pathForResource:@"name" ofType:@""];view.backgroundColor = [UIColor colorWithPatternImage[UIImage imageWithContentsOfFile:path]];

3、通过layer QuartzCore方式(推荐使用)

view.layer.contents = [[UIImage imageNamed@"name.png"].CGImage];

//如果需要透明

view.layer.backgroundColor = [UIColor clearColor].CGColor;

0 0