如何设置视图的背景图像? [英] How to set background image of a view?
本文介绍了如何设置视图的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Obj-C/Cocoa Touch/iPhone OS的初学者.
I am a beginner at Obj-C/Cocoa Touch/iPhone OS.
我希望每次调用该视图时,应用程序的背景都有不同的图像.
I wish to have a background for my app with different images everytime the the view is called.
说我有10张图片.我已经这样使用了:
Say I have 10 images. I 've used it like this:
//random image generation
NSString* imageName;
int aRandomNumber = arc4random() % 10;
imageName =[NSString stringWithFormat:@"g%d.jpg",aRandomNumber];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
NSLog(@"aRandomNumber is %d", aRandomNumber);
//random image is generated
工作正常
- 现在,说我的视图上有文本标签,并且由于图像颜色而导致文本无法正确显示. 我该如何使它变得透明? (我猜在Interface Builder中它叫alpha.)
- 说我的图片不是320x480.如何设置它以填满整个视图?
- Now, say I have text labels on my view and the text isn't displaying correctly due to image colors. How can I make it a little transparent? (I guess in Interface Builder its called alpha.)
- Say my image isn't 320x480. How do I set it to fill the entire view?
我该如何使用UIView/UIImageView?
How can I do it with UIView/UIImageView?
我在文档中找到了initWithHue:saturation:brightness:alpha:
,但是它不起作用:
I found initWithHue:saturation:brightness:alpha:
in the documentation but it's not working:
self.view.backgroundColor = [[UIColor alloc] initWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];
请帮助!
有朋友建议........
A friend suggested........
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
..........他告诉我它效率更高,因为它不会将图像保存在缓存中.
..........he told it's more efficient because it doesn't save the image in the cache.
推荐答案
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]];
更多信息(带有示例项目)
这篇关于如何设置视图的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文