一、加一個UIImageview在UIView上(還可以)
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"home"];
[self.view addSubview:imageView];
這種方式,原始圖片大小不夠(小于view的大小),會拉伸圖片,讓圖片失真,view釋放后也不會有什么內存保留。
二、通過圖片來生成UIColor來設置UIView的背景色。注意是根據圖片來生成color(不推薦)
1 . imageName方式:
如果圖片較小,并且頻繁使用的圖片,使用imageName:來加載圖片(按鈕圖片/主頁圖片/占位圖)
self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"home"]];
2 . contentOfFile方式:
如果圖片較大,并且使用次數較少,使用 imageWithContentOfFile:來加載(相冊/版本新特性)
NSString *path = [[NSBundle mainBundle]pathForResource:@"name" ofType:@"png"];
self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
以上兩種方式都會在生成color的時候消耗大量的內存(原始圖片的N倍,這個N可能會達到幾千的程度,而且如果原始圖片大小不夠,就會按照原始大小一個一個U畫過去,是不會自動拉伸的。1和2的區別:1中的color不會隨著View的釋放而釋放,而是一直存在于內存中。(再次根據這個圖片生成Color的時候,不會再次去申請內存)。而2中的color會隨著View的釋放而釋放。
三、quarCore方式(推薦)
UIImage *image = [UIImage imageNamed:@"3549"];
//推薦這樣創建image對象:UIImage *image = [UIImage imageWithContentsOfFile:path];
self.view.layer.contents = (id)image.CGImage;
//背景透明加上這一句
self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
在顯示簡單的單張圖片時,利用 UIView.layer.contents 就足夠了,沒必要使用 UIImageView 帶來額外的資源消耗。
如果對性能優化感興趣的小伙伴,可以移步這里http://blog.csdn.net/biyuhuaping/article/details/78606226