UIImage 的 setNeedsDisplay 不起作用

发布于 2024-12-06 16:22:51 字数 1553 浏览 1 评论 0原文

当触摸按钮时,我试图在 dataBuffer 的帮助下在 UIImage 上画一条线,但 drawRect: .. 方法不会被调用。 (所以,我想画的线没有出现)

我真的不知道问题出在哪里,所以我发布了更多的代码。对此感到抱歉。

顺便说一句,我正在开发一个基于视图的应用程序。

这是我的代码:

-(void) viewDidLoad{
  UIImage *image = [UIImage imageNamed:@"playView2.jpg"];
  [playView setImage:image];
  [image retain];
  offScreenBuffer = [self setupBuffer];
}

- (IBAction)buttonTouched:(id)sender {
  [self drawToBuffer];
  [playView setNeedsDisplay];    
}

- (void)drawRect:(CGRect)rect{
  CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
  UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];
  CGImageRelease(cgImage);
  [uiImage drawInRect: playView.bounds];
  [uiImage release];
}



-(void)drawToBuffer {

//                  Red  Gr   Blu  Alpha
CGFloat color[4] = {1.0, 1.0, 0.0, 1.0};

CGContextSetRGBStrokeColor(offScreenBuffer, color[0], color[1], color[2], color[3]);

CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, 10.0);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);

CGContextMoveToPoint(offScreenBuffer, 30,40);
CGContextAddLineToPoint(offScreenBuffer, 150,150);

CGContextDrawPath(offScreenBuffer, kCGPathStroke);
}

-(CGContextRef)setupBuffer{      
    CGSize size = playView.bounds.size;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGColorSpaceRelease(colorSpace);
    return context;
}

I am trying to draw a line on an UIImage with the help of a dataBuffer when a button gets touched, but the drawRect: .. methode doesn´t get called. (so, the line i want to draw does't appear)

I really don't know where the problem could be, so I posted quite a bit more code. Sorry about that.

By the way, I am working on an viewbasedapplication.

here is my code :

-(void) viewDidLoad{
  UIImage *image = [UIImage imageNamed:@"playView2.jpg"];
  [playView setImage:image];
  [image retain];
  offScreenBuffer = [self setupBuffer];
}

- (IBAction)buttonTouched:(id)sender {
  [self drawToBuffer];
  [playView setNeedsDisplay];    
}

- (void)drawRect:(CGRect)rect{
  CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
  UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];
  CGImageRelease(cgImage);
  [uiImage drawInRect: playView.bounds];
  [uiImage release];
}



-(void)drawToBuffer {

//                  Red  Gr   Blu  Alpha
CGFloat color[4] = {1.0, 1.0, 0.0, 1.0};

CGContextSetRGBStrokeColor(offScreenBuffer, color[0], color[1], color[2], color[3]);

CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, 10.0);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);

CGContextMoveToPoint(offScreenBuffer, 30,40);
CGContextAddLineToPoint(offScreenBuffer, 150,150);

CGContextDrawPath(offScreenBuffer, kCGPathStroke);
}

-(CGContextRef)setupBuffer{      
    CGSize size = playView.bounds.size;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGColorSpaceRelease(colorSpace);
    return context;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-12-13 16:22:51

这段代码在哪里? viewDidLoad 是视图控制器方法,drawRect 是视图方法。无论 playView 是什么(大概是 UIImageView?),都需要是一个自定义 UIView 子类,其中包含您的 drawRect ,并且您必须将各种项目从视图控制器传递给该子类。

Where is this code? viewDidLoad is a view controller method, drawRect is a view method. Whatever playView is (presumably a UIImageView?) needs to be a custom UIView subclass with your drawRect inside it, and you'll have to pass the various items to that from your view controller.

浮生面具三千个 2024-12-13 16:22:51

我知道这是一个老问题,但仍然有人可能会在这里遇到:

相同(或非常相似)的问题在这里得到正确答案:

drawRect 未在我的 UIImageView 子类中被调用

UIImageView 类(及其子类)不调用drawRect 如果您调用他们的setNeedsDisplay

I know this is an old question but still, someone might come across here:

the same (or very similiar) question is correctly answered here:

drawRect not being called in my subclass of UIImageView

UIImageView class (and it's sublasses) don't call drawRect if you call their setNeedsDisplay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文