Search code examples
iosswiftuiimagecgimage

UIImageView + UIImage vs CALayer + Content Efficiency


Currently, I have an UIImageView that update its UIImage every few seconds or so. This is an endless process in a very heavy UI.

Given that using CALayers where ever possible over UIView's are always lighter in weight, I am wondering if I convert the UIImageView to CALayer and setting UIImage to setting content of CALayer?


Current

//Every 2 seconds
myImageView.image = UIImage(cgImage: myCGImage)

Suggestion

//Every 2 seconds
myLayer.content = myCGImage

enter image description here

enter image description here


Since UIImageView acts slightly different, I am wondering which method would be more efficient on the CPU/GPU overall.


Solution

  • In general, UIView objects are fairly thin wrappers around CALayers. UIImageView is no exception to that. Most of the "heavy lifting" of decoding and displaying the image is done by the CALayer in both cases, so I doubt if you'll see much difference.

    UIImageView is easier to use, and the resulting code is easier to read, so unless you need to do something that requires you to use CALayers, I'd stick with UIKit objects.