因为PNGView目的在预览图片时是使用无边框+纯透明底预览,真正仿真模式查看预览效果,有助于设计师在最真实的环境还原设计效果。任何阴影、透明都表现无疑。
能不能调整大小期待以后版本升级了。
本文分析对比了各种更改UIView背景的方法。当然,背景是根据一个图片来的(非纯色)。1.图片会自动拉伸到屏幕view的大小(加一个uiimageview在uiview上面)
[objc] view plaincopy
UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.bounds]
imageView.image = [[UIImage imageNamed:@"name.png"] stretchableImageWithLeftCapWidth:left topCapHeight:top]
[view addSubview:imageView]
这种方式,如果原始图片大小不够(小于view的大小),可以拉伸,在view释放后也没有什么内存保留。
2.通过图片来生成UIColor设置view的backgroundColor(图片不会拉伸到屏幕view的大小)
1.imageNamed方式
[objc] view plaincopy
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"name.png"]]
2.contentOfFile方式
[objc] view plaincopy
NSString* path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"]
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]
这两种方式都会在生成color时占用大量的内存(原始图片的n倍,这个n可能会达到几千的程度)。而且如果图片大小不够,就会按照原始大小一个一个u画过去,也就是不会自动拉伸。1和2的区别在于,view释放后,1中的color并不会跟着释放,而是一直存在于内存中(当然,再次根据这个图片生成color时并不会再次申请内存了),而2中的color就会随着view的释放而释放。
3..quartzCore方式(图片会自动拉伸到屏幕view的大小)
[objc] view plaincopy
UIImage *image = [UIImage imageNamed:@"name.png"]
view.layer.contents = (id) image.CGImage // 如果需要背景透明加上下面这句
view.layer.backgroundColor = [UIColor clearColor].CGColor
这种方式会自动拉伸图片,而且没有额外内存占用。
综上,推荐第三种方式来根据图片设置背景色。
1.设置一般View的背景
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imgName.png"]]
imgView.frame = self.view.bounds
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth
[self.view insertSubview:imgView atIndex:0]
2.设置View的背景颜色,使用图片,效果和设置背景图片比较类似
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imgName.png"]]]
3.设置UITableView的背景
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imgName.png"]]
imgView.frame = self.view.bounds
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth
[self.tableView setBackgroundView:imgView]
4.设置UITableView的cell颜色
//方法一:
cell.contentView.backgroundColor = [UIColor redColor]
//方法二:
UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
UIView bgview = [[UIView alloc]initWithFrame:CGRectMake(0,0,1,1)]
bgview.opaque = YES
bgview.backgroundColor = [UIColor orangeColor]
[cell setBackgroundView:bgview]
//方法三:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)