Search code examples
iosswiftperformanceuiimageviewcfrunloop

CFRunLoopRun called multiple times after setting UIImageView image


The Profiler shows that CFRunLoopRun is called more than 1000 times within 20 seconds, takes up 85% and all I was doing was scrolling. The UITableView scrolling seemed laggy and I don't know what's causing the problem and calling CFRunLoopRun specifically. I have a UITableView with a custom cell that displays an image and some labels. The image is loaded on a background thread with dispatch_async(dispatch_get_global_queue,..) so I don't think that's what causing it. The imageView has rounded corners and a border, the label also have rounded corner and a background color. I'm sorry for being so unspecific but has anyone run into something similar?

Edit 1:

I looked at it again and found that the CA Render, Layout, ImageProvider takes up most of it. Here is a picture of the call tree enter image description here

Edit 2:

I've figured out that setting the imageView image calls the methods above. If I comment this one line of code where I set the image everything works fine. My question now is: why is it so slow? I've read some comments that I have to compress the image in the background first so the UIImageView doesn't have to do the job on the main thread but I couldn't come up with a good solution


Solution

  • Optimizing Image View Performance The apple docs say that the image view scales the image and that can be expensive performance-wise.

    The user can save images to the database. Setting and fetching doesn't slow anything down. My problem was that the image view had to scale the images on the main thread and that cost a lost of performance. Before setting the image I have to scale it on the background thread so the image view doesn't have to do so much work. This helped me: iOS Swift: Resize image to 200x200pt/px

    I hope I can save everyone who has the same prolem some time.