Question

I have an image stored in a RGBA buffer. Now I want to draw this data in the UIView's drawRect method:

- (void)drawRect:(CGRect)rect
{
// draw RGBA here
}

One means would be to copy the data into an UIImage (outside the drawRect method) and draw this UIImage. Is there a method to directly draw the data without having to make another copy in an UIImage?

Regards,

Was it helpful?

Solution

Using the drawrect function of a UIView for this purpose would be horrendously slow. Any time you animate the view, move it, move something else on top of it or whatever it would have to be redrawn (slow process). UIImageView is optimized for this task and it would be beneficial to use it.

But if you are dead set on drawrect then you would have to create "pixel" rectangles by taking the width and height of your UIView divided by the width and height of your image (in pixels). Then iterate through every pixel row and column and use the CGContextSetFillColor and CGContextFillRect. It would probably make performance sense to sort the array of colors into like colors and draw the same color all at once. But still much slower than UIImageView. The memory overlap of having your array and a UIImageView is not bad enough to outweigh the beneifits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top