I know that the -imageNamed: method returns a Cached UIImage, but the problem is that my image file is stored in 'Documents', and the -imageNamed: method seems to only search the Bundle... I am currently (reluctantly) using -imageWithContentsOfFile: to get my image from 'Documents' but it is not the same...Scaling up/down a UIImageView containing the resulting image is choppy and awkward. Scaling the same UIImageView containing an image created with -imageNamed: however appears very smooth. So, again: How can I get a cached UIImage from my 'Documents' if I cannot use -imageNamed:?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- SwiftUI: UIImage (QRCode) does not load after call
- how do you prevent page scroll in textarea on mobi
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Is there a google API to read cached content? [clo
- Custom Marker performance iOS, crash with result “
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
What about writing your own image cache? You have all the pieces in place, now you just need to encapsulate it and keep a record of images you've already loaded.
I made an extension of the answer provided by rpetrich and overrode the imageName: method to add more of a drop in replacement. It searches the main bundle first and then looks in the caches directory. You could of course change the caches directory to the document directory.
You can cache
UIImages
yourself just as-imageNamed:
does. It just loads them, and then holds onto them. You can hold onto them, too, using anNSDictionary
and implement your own-imageNamed:
But I'm more concerned about the trouble you're having with scaling. How are your images getting into Documents, how are you scaling them, and have you tested the same image file stored in the bundle? I doubt that
-imageNamed:
has anything to do with this. I would more suspect things like the fact that the bundle has some compression applied to it (though I don't yet have a theory on why this would matter in practice), differences in the file, or differences in how the rest of the program is behaving during scaling (causing contention on the disk or CPU). Caching is unlikely related to this issue.I'd do some profiling w/ Instruments to try to find out where the choppiness is coming from. Are you maxing out the disk, CPU, memory? What's the bottleneck?
The simplest way would be an
NSMutableDictionary
storing the cached images and a clear cache method:Note: you should call
+[UIImage clearCache]
from yourdidReceiveMemoryWarning
method. Also,clearCache
will invalidate all objects in the cache, not just unused items; aUIImage
subclass and more complicated caching mechanism would be required to remedy this.