I have a custom UITableViewCell subclass which shows and image and a text over it. The image is downloaded while the text is readily available at the time the table view cell is displayed.
From various places, I read that it is better to just have one view and draw stuff in the view's drawRect method to improve performance as compared to have multiple subviews (in this case a UIImageView view and 2 UILabel views)
I don't want to draw the image in the custom table view cell's drawRect
because
- the image will probably not be available the first time its called,
- I don't want to draw the whole image everytime someone calls drawRect.
The image in the view should only be done when someone asks for the image to be displayed (example when the network operation completes and image is available to be rendered). The text however is drawn in the -drawRect
method.
The problems:
I am not able to show the image on the screen once it is downloaded. The code I am using currently is-
- (void)drawImageInView { //.. completion block after downloading from network if (image) { // Image downloaded from the network UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextSetLineWidth(context, 1.0); CGContextSetTextDrawingMode(context, kCGTextFill); CGPoint posOnScreen = self.center; CGContextDrawImage(context, CGRectMake(posOnScreen.x - image.size.width/2, posOnScreen.y - image.size.height/2, image.size.width, image.size.height), image .CGImage); UIGraphicsEndImageContext(); } }
I have also tried:
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIGraphicsEndImageContext();
to no avail.
- How can I make sure the text is drawn on the on top of the image when it is rendered. Should calling
[self setNeedsDisplay]
afterUIGraphicsEndImageContext();
be enough to ensure that the text is rendered on top of the image?