0

I am using XCode 4.0.2 for a iOS4 project.

I have a MainView.xib and a MainViewController.

I have inserted an UIImageView in the MainView.xib with an image.

In the Identity Inspector, I give the UIImageView a custom class Diagram.

I created two new file Diagram.h and Diagram.c. In Diagram.m I have inserted

 - (void)drawRect:(CGRect)rect {
     UIGraphicsBeginImageContext(self.bounds.size); 
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGContextSetLineWidth(context, 2.0);
     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
     CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
     CGColorRef color = CGColorCreate(colorspace, components);
     CGContextSetStrokeColorWithColor(context, color);

     CGContextMoveToPoint(context, 0, 0);
     CGContextAddLineToPoint(context, 200, 200);

     CGContextStrokePath(context);
     CGColorSpaceRelease(colorspace);
     CGColorRelease(color);
 }

When I run the app, I can see the image (in UIImageView, but the drawing is not done. Perhaps is not called. How can I do?

Thank you.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
boscarol
  • 1,901
  • 2
  • 15
  • 29
  • If you remove the image, can you see the drawing that takes place in your `drawRect` method? i.e. Is the `UIImageView` drawing the image on top of your custom drawing. –  Jul 20 '11 at 14:41
  • No, there is no drawing under the image. – boscarol Jul 20 '11 at 14:51

1 Answers1

1

The answer is in the official Apple docs

Special Considerations

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: on a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

You can see more detail on this question

Community
  • 1
  • 1