问题
I am writing an app that takes a picture using the iDevice camera and stores it on the file system as a PNG. When starting my app, I load a UICollectionView with the images found on the file system.
The issue I am facing is that the load time for the app (even when its only loading 6 images is approximately 4 seconds which is unacceptable. I have implemented GCD to load images on a background thread (which keeps the UI snappy) however I really want the app to start far quicker with images loaded.
My thoughts are:
I suspect the initWithContentsOfFile
is taking ages to load the full size image.
I thought about generating a separate thumbnail image when a picture is taken and load that instead.
Ultimately when I look at Apple's Photo app, it loads "instantly" and has 10's of pictures in view.
Does anyone have any suggestions for how I can load the images faster or at least appear to?
Thanks!
回答1:
Check this sample code and try to get some idea from it https://developer.apple.com/library/ios/samplecode/lazytableimages/introduction/intro.html
回答2:
Try this
Load image in background thread and set in main thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
UIImage *image = [UIImage imageWithContentsOfFile:frontPath];
dispatch_async(dispatch_get_main_queue(), ^{
[self.frontButton setBackgroundImage:image forState:UIControlStateNormal];
});
});
来源:https://stackoverflow.com/questions/22631394/performance-uicollectionview-loading-time-very-slow