views:

729

answers:

1

Can anyone help me with this. I need to do custom drawing code in -drawRect so I subclassed UIImage. How would I initialize my custom UIImage with an image? Say I override the imageName method, what would I need to do in this method? After initializing can I add it to a UIImageview like so initwithImage:?

A: 

If you're going to do custom drawing, don't subclass UIImage, subclass UIView, and add your subclass view to wherever you want the custom drawing to be. Do all of your drawing directly in the -drawRect: method of UIView. An example of how to draw a diagonal blue line:

-(void) drawRect:(CGRect)rect {
  CGContextRef g = UIGraphicsGetCurrentContext();
  CGContextMoveToPoint(g,self.frame.origin.x,self.frame.origin.y);
  [[UIColor blueColor] setStroke];
  CGContextAddLineToPoint(g,CGRectGetMaxX(self.frame),CGRectGetMaxY(self.frame));
}
Ed Marty