1

When should someone use UIImage's method init(contentsOfFile:), when is appropriate to use init(named:) and when init(data:)? I read about it and it seems the only difference is that with init(named:) image stays around for a while and with init(contentsOfFile:) is it deallocated as soon as possible eg when it is not on screen anymore. Please correct my assumptions if they are wrong. Not sure what init(data:) is good for.

1 Answer 1

3

you use init(contentsOfFile:) when you for example have a path to an image stored somewhere on your phone.

you use init(named:) when you have an image with the passed name in your application bundle.

and you use init(data:) when you have some image data (for example you downloaded the image data from some web source) and want to create the image from that data.

one important difference between the three initializers is that only the imageNamed initializer caches the returned image object!

you should really consult the official documentation for questions like this though: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/doc/uid/TP40006890-CH3-SW11

Sign up to request clarification or add additional context in comments.

4 Comments

I think that I can specify path to app bundle. So it is like a shortcut method?
kind of. there is an important difference though: initWithContentsOfFile does not cache the image object. imageNamed does.
so use contentsOfFile for big images and the one that wont be used frequently, right?
as the documentation states: If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.