iOS-UIView-设置背景图片4种方式

一 . 设置UIView的背景图片

1.将图片作为UIView的背景色,该方法过于占内存,不建议使用。

//1.imageNamed方式

self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"image.jpg"]];

//2.方式    NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];

self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageWithContentsOfFile:path]];

//这两种方式都会在生成color时占用大量的内存。如果图片大小不够,就会平铺多张图片,不会去拉伸图片以适应View的大小。

//在View释放后,1中的color不会跟着释放,而是一直存在内存中;2中的color会跟着释放掉,当然再次生成color时就会再次申请内存

2.在UIView上再添加一个UIImageView显示图片作为UIView的背景图片

注意:如果有点击事件的话, userInteractionEnabled用户交互设置为YES。

3.iOS视图都是一个图层,最先放置的图层就会在最底层,如此最先给UIView添加一个UIImageView就可以作为UIView的背景图片使用啦

4.其他方式(推荐)

NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];       UIImage *image = [UIImageimageWithContentsOfFile:path];

self.view.layer.contents = (id)image.CGImage;

//注意: 要写清楚后缀,即使是”.png”。

时间: 2024-08-04 14:11:30

iOS-UIView-设置背景图片4种方式的相关文章

UIView设置背景图片几种方式

一 . 设置UIView的背景图片 1.将图片作为UIView的背景色,该方法过于占内存,不建议使用. //1.imageNamed方式 self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"image.jpg"]]; //2.方式    NSString *path = [[NSBundlemainBundle]pathForResource:@"image"o

UIView 设置背景图片

http://blog.csdn.net/qijianli/article/details/7777268 项目中,可能需要我们为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,那么该如何实现呢? 第一种方法: 利用的UIView的设置背景颜色方法,用图片做图案颜色,然后传给背景颜色. UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bgImg.png"]; UIVie

Qt 设置背景图片3种方法(简单,但值得仔细体会)

方法1. setStylSheet{"QDialog{background-image:url()"}}  //使用styleSheet 这种方法的好处是继承它的dialog都会自动设置背景,例如更换皮肤就是一个不错的选择方法2. QPalette pal; pal.setBrush(QPalette::Background,QBrush(QPixmap("")));            this->setPalette(pal);方法3.在paintEve

uiscrollview UIView 设置背景图片 swift 不用colorWithPatternImage

colorWithPatternImage 非常占用内存,而且在swift中去掉了这个方法. sizeView.layer.contents = UIImage(named:"slect_bg")!.CGImage

iOS下uiview和uiscrollview设置背景图片的源码

1.uiscrollview 设置背景图片 // Setup the Scroll ViewUIScrollView*tempScrollView=(UIScrollView*)self.view;tempScrollView.contentSize=CGSizeMake(320,720); // Set Stationary Background, so that while the user scroll the background is// fixed.UIImage*img =[UII

设置背景图片的两种方式,并解决手机端背景图片高度自适应问题

1 设置背景图片的两种方式: 方式一: <img src="../img/10.jpg"/ class="back" id="Background"> .back{ position: fixed; width: 100%; height: 100%; display: block; z-index: -100; } 方式二:div class="body" id="Background">

两种 设置 背景图片方法

两种设置背景图片方法 //    UIView *navigationbarTitleTupian =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 30)]; //    UIImageView *imageTitleTupian =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg.png"]]; // //    [navigationbarTitleTupian

【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系

(1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器可以自定义不同的状态栏如下: -(BOOL)prefersStatusBarHidden{ } -(UIStatusBarStyle)preferredStatusBarStyle{ } --如果我们需要交给UIApplication对象管理(iOS6之前就是),则需要如下设置: 先在info.plist中把默认的设置修改掉: 然后直接用如下代码即可: - (void)view

002-UIImageView和UIButton对比 UIImageView的帧动画 格式符补充 加载图片两种方式 添加删除SUBVIEW

一>.UIImageView和UIButton对比 显示图片 1> UIImageView只是一种图片(图片默认会填充整个UIImageView)  image\setImage: 2> UIButton能显示2种图片 * 背景 (背景会填充整个UIButton)  setBackgroundImage:forState: * 前置(覆盖在背景上面的图片,按照之前的尺寸显示)  setImage:forState: * 还能显示文字 点击事件 1> UIImageView默认是不能