Load image from bundle with iOS Load image from bundle with iOS objective-c objective-c

Load image from bundle with iOS


If that does not work, try

[UIImage imageNamed:@"yourbundlefile.bundle/imageInBundle.png"];

Best


That is correct, imageNamed: will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.


Apple has thankfully provided a proper API for this from iOS 8 onwards, imageNamed:inBundle:compatibleWithTraitCollection:.

It's used like so:

[UIImage               imageNamed:@"image-name"                         inBundle:[NSBundle bundleForClass:self.class]    compatibleWithTraitCollection:nil]

or in swift:

let image = UIImage(named: "image-name",                    inBundle: NSBundle(forClass: type(of:self)),                    compatibleWithTraitCollection: nil)

or in swift 5:

let image = UIImage(named: "image-name",                    in: Bundle(for: type(of:self)),                    compatibleWith: nil)