三种设置UIview背景图片的方法
// 1 利用背景颜色(内存占用40-50M),将图片作为UIView的背景色,如果图片大小不够,就会平铺多张图片,不会去拉伸图片以适应View的大小,该方法过于占内存,不建议使用。
//从nsset里面加载图片
UIImage*image = [UIImage imageNamed:[NSString stringWithFormat:@"background"]];
UIColor*backcolor = [UIColor colorWithPatternImage:image];//在View释放后,1中的color不会跟着释放,而是一直存在内存中
self.view.backgroundColor = backcolor;//[self.view setBackgroundColor:backcolor];
//从boundle里面加载图片,没有缓存
NSString *path = [[NSBundle mainBundle]pathForResource:@"background"ofType:@"jpg"];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
//color会跟着释放掉,当然再次生成color时就会再次申请内存
//2 sendSubviewToBack insertSubview:imgView atIndex:0 设置法(47M)
UIImageView*imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
imgView.image= image;
[self.viewaddSubview:imgView];
[self.view sendSubviewToBack:imgView];//会将设置的背景tsubView放在根视图的这一层,所有subView的s最底层;
[self.view insertSubview:imgView atIndex:0];
//3 设置layer的content(推荐)
self.view.contentMode = UIViewContentModeScaleAspectFit;
self.view.layer.contents = (__bridge id _Nullable)(image.CGImage);
// 4 毛玻璃效果的实现
UIVisualEffectView*visualEFView =[ [UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
visualEFView.frame=self.view.frame;
visualEFView.alpha=0.5;
[self.viewaddSubview:visualEFView];
}
网友评论