1

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;
}
dandan78
  • 13,328
  • 13
  • 64
  • 78
Kito
  • 1,375
  • 4
  • 17
  • 37

2 Answers2

1

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • oh, sorry but i forgot to mention this, but you are right, all the code is in viewcontroller.m , and playView is an UIImageView. ;-) hmmm ok, is there no over way than changing my UIImageView to an UIView subclass, because i already have quite a lot of code based on my UIImageView, which i would not want change if possible. :D – Kito Sep 26 '11 at 17:30
  • Well, not sure what youre trying to achieve with the buffering, but you could either pass your buffered image to the imageview as it's image, have a separate view for your lines which is a subview of your image view, or try to subclass uiimageview, not sure that is a good idea, though. – jrturton Sep 26 '11 at 17:45
  • well actually, i want to do this : (one of my older posts) http://stackoverflow.com/questions/7547807/how-to-set-rgba-color-of-a-pixel-of-an-uiimage but i don´t really know how to do it :P you don´t have a good idea for this??? :D – Kito Sep 26 '11 at 18:04
  • yes :D but i needed more detail for some steps. so i thought that i need this "setneedDisplay", "drawRect" and so on. Or do i don´t need to use this for the solution, mentioned in the other thread?? if not, how do i realize the editing part??? – Kito Sep 26 '11 at 20:15
1

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.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124