UIImageView causes lag first time they are displayed
I have a couple of UIViews with UIImageViews on them. When I display a view the first time, there is a noticeable lag (0.5 second or so) before it is shown. When shown again everything works.
I have managed to work around this by adding all views and then removing them. Like this:
[self performSelectorOnMainThread:@selector(addAndRemoveAllViews)
withObject:nil
waitUntilDone:NO];
The function loops over all views and [addSubview:view]
followed by [view removeFromSuperview]
. This seems to trigger the images to load while not being shown.
Questions:
- This all feels like a workaround to me. Is there a bette开发者_如何学运维r approach to handle these types of things?
- Is it possible to get a callback or something when it's completed? I want to run an animation when done.
Update:
Images are created from NSData
like this:
[[UIImageView alloc] initWithImage:[UIImage imageWithData:rawData]];
It seems very likely the image is larger than the image view. If possible shrink it down to the size the image view needs.
The initial delay is loading the image from storage, after that you are using a cached version which is why it is faster.
精彩评论