设置自定义UIView的背景图像

时间:2011-08-02 10:30:45

标签: iphone objective-c ios ipad

我正在使用OpenGL开发一个绘图应用程序。

我使用“CUSTOM UIView”在屏幕上绘图,而另一个UIImageView用于设置背景图像。看看截图,清楚地了解它。我的问题是我无法在任何一个视图上设置背景图像。我必须使用不同的纹理作为绘图板,我必须在其上绘画。任何人都可以建议这种方法中的错误吗?

Imagelink http://img835.imageshack.us/img835/9794/screenshot20110802at345.png

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

- (id)initWithCoder:(NSCoder*)coder 
{

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Rough_White_Paper_with_tape.png"]];
    self.backgroundColor = background;

      [background release];     
}

8 个答案:

答案 0 :(得分:38)

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];

答案 1 :(得分:30)

使用此

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

答案 2 :(得分:26)

   - (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;

      }

你只需添加回归自我;它对我来说非常好......

答案 3 :(得分:8)

试试这个:

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

答案 4 :(得分:3)

试试这个:

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

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

这是在swift 2

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

答案 7 :(得分:0)

使用此

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