21
UIImage *img = [[UIImage alloc] initWithContentsOfFile:@"xx.jpg"]
UIImage *img = [UIImage imageNamed:@"xx.jpg"]

In the second type will the image get cached ?
Whereas the in the first type the images doesn't get cached?

raghul
  • 1,008
  • 1
  • 15
  • 33

4 Answers4

34
  • The -initWithContentsOfFile: creates a new image without caching, it's an ordinary initialization method.

  • The +imageNamed: method uses cache. Here's a documentation from UIImage Reference:

    This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

    UIImage will retain loaded image, keeping it alive until low memory condition will cause the cache to be purged.

Update for Swift: In Swift the UIImage(named: "...") function is the one that caches the image.

Lukas Kalinski
  • 2,213
  • 24
  • 26
iHunter
  • 6,205
  • 3
  • 38
  • 56
  • will caching cause additional resources to be used or will it be just a pointer to that object? – raghul Dec 27 '11 at 13:17
  • 1
    @raghul UIImage will just retain your existing image, keeping it alive after you release it until low memory condition will cause the cache to be purged. – iHunter Dec 27 '11 at 13:29
  • 4
    I've simulated low memory, but the cache didn't purged in iOS 10 – Yitzchak Sep 17 '16 at 20:39
9

Just wanted to leave this here to help deal with the pathnames issue. This is a method that you can put on a UIImage category.

+(UIImage *)imageNamed:(NSString *)name cache:(BOOL)cache {
    if (cache)
        return [UIImage imageNamed:name];
    name = [[NSBundle mainBundle] pathForResource:[name stringByDeletingPathExtension] ofType:[name pathExtension]]; 
    UIImage *retVal = [[UIImage  alloc] initWithContentsOfFile:name];
    return retVal;
}

If you don't have an easy way to switch to cached, you might end up just using `imageNamed. This is a big mistake in most cases. See this great answer for more details (and upvote both question and answer!).

Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 1
    If you want this to not break if there is more than one period, you could use `newName = [[NSBundle mainBundle] pathForResource:[name stringByDeletingPathExtension] ofType:[name pathExtension]];` and `UIImage *retVal = [[UIImage alloc] initWithContentsOfFile:newName];` – lnafziger Dec 25 '12 at 17:23
  • 1
    Note: in the case where *only* the @2x version of the image is included in the bundle this will break as `pathForResource` will not check for @2x. Otherwise, great solution. – Joel Sep 08 '14 at 17:05
  • 1
    You saved my life! – Yitzchak Sep 17 '16 at 21:02
  • @Yitschak glad it's helpful, thanks for commenting! If you happen to be using Swift, though, please check out Haneke, too. – Dan Rosenstark Sep 19 '16 at 18:27
  • https://github.com/Haneke/HanekeSwift and https://github.com/Haneke/Haneke. Worth the time to set it up – Dan Rosenstark Sep 19 '16 at 18:37
1

@Dan Rosenstark answer in swift..

extension UIImage {

    static func imageNamed(name: String, cache: Bool) -> UIImage? {
        if (cache) {
            return UIImage(named: name)
        }

        // Using NSString for stringByDeletingPathExtension
        let fullName = NSString(string: name)
        let fileName = fullName.stringByDeletingPathExtension
        let ext = fullName.pathExtension
        let resourcePath = NSBundle.mainBundle().pathForResource(fileName, ofType: ext)

        if let path = resourcePath {
            return UIImage(contentsOfFile: path)
        }
        return nil
    }
}
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
0

Correct, the second item is cached.

Nico
  • 3,826
  • 1
  • 21
  • 31