Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image resources for iOS

I'm probably missing something obvious here, yet I've been unable to solve the following problem:

I have a project with image resources for both normal and retina screens, like someimage.png and [email protected], which are stored in a separate bundle. When I build the project, Xcode automatically packs them into a single multipage tiff (imageName.tiff), I've checked it in finder - it is actually multipage tiff with both images. However, here comes a problem: I struggle to load appropriate resource.

What I do is:

    NSString * imageName = ... ;      NSLog(@"imageName: %@", imageName);      UIImage * someImage = [UIImage imageNamed: imageName]; 

Also I fave auxiliary method, which returns bundle with resources:

   +(NSBundle *) resourcesBundle    {          NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyResourcesBundle" withExtension:@"bundle"]];          return bundle;    } 

I've tried following for imageName:

imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"png"]; 

in this case i have null for imageName.

imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"tiff"]; 

in this case actuall image path is returned, however it only works if I use imageWithContentsOfFile instead of imageNamed, and it doesn't take appropriate resource: it loads resource for retina despite the type of screen.

If I ommit filetype (as I did before adding @2x resources, and it worked ok, and was the first thing I tried and was sure it would work)

imageName = [NSString stringWithFormat: @"%@/%@",                                   @"MyResourcesBundle.bundle"",                                   @"someimage" ]; 

nothing get's loaded.

Adding ".tiff" extension have the same effect as pathForResource: - resource for retina is loaded, disregarding resource for non-retina screen.

So what am I missing? What's the correct way of loading images?

like image 861
Vladimir Avatar asked Sep 03 '12 08:09

Vladimir


People also ask

What is 1x 2x and 3x in iOS?

1x, 2x, and 3x images allow developers and Apple to optimize app presentation based on the user's device, whether an entry-level iPhone or the most expensive iPad. Conceptually, 1x, 2x, and 3x images are the same image -- simply at different sizes.

What UI tool does Apple use?

SwiftUI helps you build great-looking apps across all Apple platforms with the power of Swift — and surprisingly little code. You can bring even better experiences to everyone, on any Apple device, using just one set of tools and APIs.

Can you drag and drop files to iPhone?

Copy files from your computer to your iOS or iPadOS appDrag and drop files from a folder or window onto the Documents list to copy them to your device. You can also click Add in the Documents list in iTunes, find the file or files you want to copy from your computer, and then click Add.


2 Answers

Have you tried to simply load the image using:

UIImage * someImage = [UIImage imageNamed: @"someimage"]; 

(assuming your have an image named 'someimage' in you project, for example someimage.png)

The code will automatically pick retina/non-retina versions depending on the platform.

If the problem is that the TIFF are created, check:

XCode Combine high-resolution artwork

In latest version of XCode, go to the Editor menu, then select "validate settings", which should remove that artwork combination.

like image 90
Resh32 Avatar answered Oct 02 '22 19:10

Resh32


Multipage TIFFs are only for OSX; they don't work on iOS.

So you need to stop trying to access resources that are, by their very nature, inaccessible, and do things the right way!

You need to open the project that generates the external resources bundle, select the target, go to Build Settings and then the Deployment sub-heading, and set "Combine High Resolution Artwork" to No. Then recompile the external resources bundle.

In your main project you should now be able to read in the PNGs in the normal manner.

like image 29
Wildaker Avatar answered Oct 02 '22 21:10

Wildaker