Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView performance: opaque, backgroundColor, clearsContextBeforeDrawing?

I'm displaying opaque PNGs with UIImageViews inside of a superview with a white background color. What's best for performance?

UIImageView Defaults

opaque = NO, backgroundColor = nil, clearsContextBeforeDrawing = YES.

iOS Developer Library: UIView Class Reference

  1. UIView Class Reference: backgroundColor says, "[nil] results in a transparent background color." If I set a UIViews opaque property to YES, must I also set its backgroundColor to [UIColor clearColor], or is that extra line of code & processing unnecessary? I.e., is [UIColor clearColor] considered opaque (not transparent)?

  2. Does the value of clearsContextBeforeDrawing matter for opaque views?

    The comments for clearsContextBeforeDrawing in UIView.h say it's ignored for opaque views.

    But, UIView Class Reference: clearsContextBeforeDrawing says:

    If the view’s opaque property is also set to YES, the backgroundColor property of the view must not be nil or drawing errors may occur.

    Which is it?

Similar Questions

  • Is UIView's opaque property with a value of YES in conflict with its backgroundColor property with a value of [UIColor clearColor]?
  • Cocoa/iPhone: BackgroundColor and Opaque Properties
like image 839
ma11hew28 Avatar asked Nov 05 '11 03:11

ma11hew28


2 Answers

Assuming that your PNGs always fills the entire UIImageView, you should get the best performance using:

opaque = YES, clearsContextBeforeDrawing = NO. In this mode backgroundColor is irrelevant. The pixels are simply replaced with the new image data.

For transparent PNGs on a single-color background, the fastest will be:

opaque = YES, clearsContextBeforeDrawing = YES, and backgroundColor matching whatever you need. In this case [UIColor whiteColor].

like image 106
Gil Avatar answered Nov 14 '22 01:11

Gil


If you set UIView's opaque property to YES, you must ensure your drawing completely fills the view with no transparency. So, you need to set a white background colour (a clear one won't work, because that's not opaque).

I don't think you want to change the defaults for your image view, but you should ensure that your PNGs do not have any transparency (that is, have no alpha channel; you can check this in Preview's image inspector; ensure it doesn't say 'Has Alpha'.). I believe UIImageView will do the right thing if you give it an opaque image.

Depending on what's in the background, you might get better performance with opaque = YES and a white background for your image views, (to prevent redrawing parts of the parent view) but I wouldn't start that way.

like image 23
Jesse Rusak Avatar answered Nov 13 '22 23:11

Jesse Rusak