what is the difference between UIImageView and drawInRect?

Rajkanth picture Rajkanth · Oct 1, 2010 · Viewed 7.8k times · Source

I want to display so many images in table cells. I knew two methods to show an image.

One is creating an instance to UIImageView and show it

CGRect rect=CGRectMake(x,y,width,height);
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
[image setImage:[UIImage imageNamed:@"sample.jpg"]];

Another method is,

CGRect rect=CGRectMake(x,y,width,height);
[[UIImage imageNamed:@"sample.jpg"] drawInRect:rect];

Now, my question is, what is the difference between these two? Which one is efficient? Or someother function is available better than this?

Thanks in advance....

Answer

rpetrich picture rpetrich · Oct 1, 2010

Using the drawInRect: method has CoreGraphics draw the image into the active CGContext using the CPU. Assuming you're in a UIView's drawRect: method, this will paint the image into the view's buffer.

UIImageView uses whichever image is assigned to it as it's backing buffer instead of using the slower drawRect:. The GPU then references this buffer directly when the screen is composited via QuartzCore.