设置自定义UIView的背景图像

26 iphone objective-c ipad ios

我正在使用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];     
}
Run Code Online (Sandbox Code Playgroud)

ram*_*ram 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];
Run Code Online (Sandbox Code Playgroud)


Saa*_*aad 30

用这个

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default"]];
Run Code Online (Sandbox Code Playgroud)


Boj*_*vic 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;

      }
Run Code Online (Sandbox Code Playgroud)

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


Pau*_*tin 8

试试这个:

UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Rough_White_Paper_with_tape.png"]];
[self addSubview:background];
[self sendSubviewToBack:background];
[background release];
Run Code Online (Sandbox Code Playgroud)