设置UIImageView映像后,CFRunLoopRun多次调用

时间:2016-03-03 07:17:25

标签: ios swift performance uiimageview cfrunloop

Profiler显示CFRunLoopRun在20秒内被调用超过1000次,占85%,而我所做的只是滚动。 UITableView滚动似乎很迟钝,我不知道导致问题的原因和具体调用CFRunLoopRun。 我有一个带有自定义单元格的UITableView,它显示图像和一些标签。使用dispatch_async(dispatch_get_global_queue,..)将图像加载到后台线程上,因此我不会认为导致它的原因。 imageView有圆角和边框,标签也有圆角和背景色。 我很抱歉这么不明确,但是有没有人遇到类似的东西?

编辑1:

我再次查看它,发现CA Render,Layout,ImageProvider占用了大部分内容。这是调用树的图片 enter image description here

编辑2:

我发现设置imageView图像会调用上面的方法。如果我评论这一行代码,我设置图像一切正常。 我现在的问题是:为什么这么慢?我已经阅读了一些评论,我必须首先在后台压缩图像,所以UIImageView不必在主线程上完成工作,但我无法想出一个好的解决方案< / p>

1 个答案:

答案 0 :(得分:0)

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.