How to draw UIImage or directly in -drawRect :?

I have a UIImage that I want to draw on a UIView . But instead of creating a UIImageView and adding this as a preview, I want to overwrite -drawRect: and draw the UIView directly.

For example, my code looks like this:

 - (void)drawRect:(CGRect)rect { CGContextRef myContext = UIGraphicsGetCurrentContext(); UIImage *img = [UIImage imageNamed:@"foo.png"]; // how to draw the directly into the view now? } 
+46
iphone cocoa-touch uiimage drawrect
Nov 02 '09 at 21:27
source share
2 answers

Call [img drawInRect:rect]; .

+83
Nov 02 '09 at 21:34
source share

By the way, you should not load the image file into the drawRect method. Because the method is called whenever a view needs to be updated. Therefore, this (of course, the boot procedure) can be called many times during operation. (In addition, the OS2.x imageNamed method has an error - it did not cache the image or leak.)

Therefore, you are better off loading the file into the initialization method rather than into drawRect.

+30
Nov 04 '09 at 5:46
source share



All Articles