ios 为自定义 UIView 设置背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6910651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:55:36  来源:igfitidea点击:

Set background Image for a Custom UIView

iphoneobjective-ciosipad

提问by Saad

I am developing a paint application using OpenGL.

我正在使用 OpenGL 开发绘画应用程序。

I am using "CUSTOM UIView" for drawing on a screen and another UIImageView to set background image to it. Have a look at screenshot to understand it clearly.. My problem is I am not able to set background image on either of the view.. I have to use different textures as a drawing board on which I have to Paint. Can anyone suggest what is incorrect in this approach??

我正在使用“CUSTOM UIView”在屏幕上绘图,并使用另一个 UIImageView 为其设置背景图像。看一下屏幕截图以清楚地理解它.. 我的问题是我无法在任何一个视图上设置背景图像.. 我必须使用不同的纹理作为我必须在其上绘画的绘图板。谁能建议这种方法有什么不正确的地方?

Imagelink

图片链接

I have used this code for custom UIView but not getting success.. but it shows white default background only..

我已将此代码用于自定义 UIView 但没有成功..但它仅显示白色默认背景..

- (id)initWithCoder:(NSCoder*)coder {
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Rough_White_Paper_with_tape.png"]];
    self.backgroundColor = background;
    [background release];     
}

回答by ram

NSURL *imgUrl=[[NSURL alloc] initWithString:@"http://img835.imageshack.us/img835/9794/screenshot20110802at345.png"];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];

imageView = [[UIImageView alloc] initWithImage:img];

[self addSubview:imageView ];
[self sendSubviewToBack:imageView ];

[imageView release]; [imgUrl release];

回答by Saad

use this

用这个

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default"]];

回答by Bojan Bozovic

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Rough_White_Paper_with_tape.png"]];
        self.backgroundColor = background;
        [background release];
    }
    return self;
}

You just had to add return self; It works perfectly fine for me...

你只需要添加 return self ;它对我来说非常好......

回答by Paul Warkentin

Try this:

尝试这个:

UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Rough_White_Paper_with_tape.png"]];
[self addSubview:background];
[self sendSubviewToBack:background];
[background release];

回答by Surjit Joshi

Try this:

尝试这个:

 UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed :@“abc.png"]];
 UIView *myUIView = [[UIImageView alloc] initWithFrame :CGRectMake(10,10,250,250)];
 myUIView = myImageView;

回答by clqiao

set background with UIImageView is not graceful。 should use following API:

用 UIImageView 设置背景不优雅。 应该使用以下 API:

self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

回答by Shahryar

this is in swift 2

这是在 swift 2

var background = UIImageView(image: UIImage(named: "image.jpg"))
self.addSubview(background)
self.sendSubviewToBack(background)

回答by Himanshu Mahajan

Use this

用这个

self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];