一般我们设置背景图片有两种方法:
//不释放内存,比较占内存,有缓存,加载常用的图片
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设置View的backgroundColor (不推荐)
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;