Load image from bundle with iOS

Load image from bundle with iOS

I added to my project a .bundle folder filled with some images. 
Is it correct  refer to this image directly writing something like ?:
[UIImage imageNamed:@"imageInBundle.png"];

Which is the best method to access and use these images ? 

Solutions/Answers:

Answer 1:

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.

Answer 2:

If that does not work, try

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

Best

Answer 3:

1) If you are working with bundles go to bundle target and set COMBINE_HIDPI_IMAGES to NO.
In another case the image will be converted to tiff format.

2) try this code:

NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"YourBundle" withExtension:@"bundle"]];
NSString *imagePath = [bundle pathForResource:@"imageInBundle" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

Answer 4:

Apple has thankfully provided a proper API for this in iOS 8, 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: self),
                    compatibleWithTraitCollection: nil)

Answer 5:

This Method returns image anywhere in the xcode project:=>

+(UIImage*)returnImageFromResourceBundle:(NSString*)nameOfImage
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
    NSString *imageString = [[NSBundle bundleWithPath:bundlePath] pathForResource:nameOfImage ofType:@"png"];

    UIImage *retrievedImage = [[UIImage alloc] initWithContentsOfFile: imageString];
    return retrievedImage;
}

Answer 6:

Swift 3

let myImage = UIImage(named: "nameOfImage", in: Bundle(for: type(of: self)), compatibleWith: nil)

I can’t get current bundle so i do this:

Bundle(for: type(of: self))

Answer 7:

Since I had to figure this out for Swift, thought I would add also….

if let imagePath = NSBundle.mainBundle().pathForImageResource("TestImage.png")     
{
    imageView.image = NSImage(contentsOfFile: imagePath)
}

Where I have TestImage.png in the Supporting Files group inside my bundle.

Answer 8:

For Swift 3

let prettyImage = UIImage(named: "prettyImage",
            in: Bundle(for: self),
            compatibleWith: nil)

Answer 9:

Swift version

@AndrewMackenzie’s answer didn’t work me. This is what did work

let image = UIImage(named: "imageInBundle.png")
imageView.image = image

My full answer is here.

Answer 10:

A quick way to do it if you are going to use it a couple of times in the same viewController:

Get the image anywhere in the same class as the method below:

// this fetches the image from: MyBundle.bundle/folder/to/images/myImage.png
UIImage *myImage = [self imageFromBundle:@"MyBundle" withPath:@"folder/to/images/myImage.png"];

Method that fetches the image:

- (UIImage *)imageFromBundle:(NSString *)bundleName withPath:(NSString *)imageName
{
    NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
    NSString *imagePath = [bundle pathForResource:imageName ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    return image;
}

Or simply call it straight out of the bundle:

UIImage *myImage = [UIImage imageNamed:@"MyBundle.bundle/folder/to/images/myImage.png"];

References