(iPhone) Use external image in UIImage imageNamed

I am downloading two images from the web Apple.png and Apple@2x.png. I want to use [UIImage imageNamed:@”Apple.png”] so it can use the build in features to detect whether it should display either Apple.png or Apple@2x.png.

Now where do i store these images? I read the following in the documentation:

  • Clear Background color on UIImageView?
  • iPhone - How to symbolicate/translate/read device crashlog with XCode 4
  • Cast to a control using the control name as a string
  • Trying to overwrite sqlite database in iPhone app update
  • Objective-C Library for Sunrise and Sunset?
  • iOS 8 upside down orientation, XCode option enabled, still doesn't work
  • The name of the file. If this is the
    first time the image is being loaded,
    the method looks for an image with the
    specified name in the application’s
    main bundle.

    Ah so the Application’s Main Bundle is the way to go. This is what my code looks like:

    NSString      *directory   = [[NSBundle mainBundle] bundlePath]; 
    NSString      *path        = [directory stringByAppendingPathComponent:imageName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:path contents:dataFetcher.receivedData attributes:nil];

    I checked if the file was created in the folder of the path and that was correct. I also dragged an Example.png in my project file just to see if it got stored in the same folder and that also was correct.

    However, [UIImage imageNamed:@”Apple.png”], still fails to fetch the images. Does anyone have an idea?


    I made a category for UIImage:

    @implementation UIImage (ImageNamedExtension)

    + (UIImage *)imageNamed:(NSString *)name fromDirectory:(NSString *)directory {
        NSString      *name2x      = [name stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", [name pathExtension]] withString:[NSString stringWithFormat:@"@2x.%@", [name pathExtension]]];
        NSString      *path        = [directory stringByAppendingPathComponent:name];
        NSString      *path2x      = [directory stringByAppendingPathComponent:name2x];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) {
            if ([fileManager fileExistsAtPath:path2x]) {
                return [UIImage imageWithContentsOfFile:path2x];
        return ([fileManager fileExistsAtPath:path]) ? [UIImage imageWithContentsOfFile:path] : nil;

    That did the trick eventualy. Thanks all for the help.

    5 Solutions Collect From Internet About “(iPhone) Use external image in UIImage imageNamed”

    You cannot use UIImage‘a +imageNamed: method with pictures downloaded by the app. That method looks for images in the app bundle indeed, but your app is not allowed to change its own bundle at run time.

    Download the images (asynchronously of course) to the store local to your app (ie, your sandbox), and reference them locally. You can then use NSDocumentDirectory or NSCachesDirectory to always obtain a reference to their location.

    Are the files being created with appropriate permissions for you to access them? Did you make sure to close the file after saving it?

    You cannot modify your bundle at runtime. I also think that imageNamed uses something like a resources map initialized within the app containing the bundle resources references.

    Try this instead:


    I think the best solution is to make in Library folder your own bundle e.g. images.bundle. There you can strore your images for both resolutions. To access that images you can use pathForResource:ofType: method of NSBundle class. It returns appropriate images. Than init image with returned path.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSURL *libraryURL = [fileManager URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
    if (libraryURL == nil) {
        ALog(@"Could not access Library directory\n%@", [error localizedDescription]);
    } else {
        NSURL *folderURL = [libraryURL URLByAppendingPathComponent:@"images.bundle"];
        DLog(@"Future bundle url = %@",folderURL);
        if (![fileManager createDirectoryAtURL:folderURL withIntermediateDirectories:YES attributes:nil error:&error]) {
            ALog(@"Could not create directory %@\n%@", [folderURL path], [error localizedDescription]);
        } else{
            NSBundle *bundle = [NSBundle bundleWithURL:folderURL];
            // put image and image@2x to the bundle;
            [bundle pathForResource:yourImageName ofType:@"png"];