Search code examples
iphoneiosquartz-2d

Is this the best way to draw large images to a UIView?


The app I'm working on requires me to draw images to a UIView. These images are large (some are 1024x768), and I'd like to be sure my drawing code isn't sub-optimal. My drawRect method in my UIView looks like this:

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextClearRect(context, rect);

  CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, 550);
  CGContextConcatCTM(context, flipVertical);
  CGImageRef shadowImage = [[UIImage imageNamed:@"someimage.png"] CGImage];
  CGContextDrawImage(context, CGRectMake(162, -174, 700, 700), shadowImage);
  CGContextConcatCTM(context, flipVertical);
}

There is other drawing done here too, but I just want to be sure that the image drawing code isn't going to cause problems, and I haven't neglected to call an important method or implemented this incorrectly.


Solution

  • I have found that this method is sub-optimal because of the use of

    [UIImage imageNamed:@"someimage.png"]
    

    This method loads the image into memory then caches it for later use. Because the images I am working with are large, and there are several of them, the use of this method can cause excessive memory usage.

    I've written a short post with more detail.