4

I build my project using iOS 7.1 and try to load UIImage view with image that is stored in the /images/cars/car_1.png

All images are located in the folder images as on picture below in project tree:

enter image description here

So it works perfect for iOS 7.1 and Xcode 5, but when I try to use Xcode 6 and iOS 8 the UIImage instance is equal nil when I try crate image.

UIImage *image = [UIImage imageNamed:@"/images/cars/car_1.png"];

po image

nil (for iOS 8)

it can be also

UIImage *image = [UIImage imageNamed:@"/images/sport-cars/car_1.png"];

as you can see the name of resources is the same car_1.png but it is ok because they are in different resources folders not in the bundle folders.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • UIImage *image = [UIImage imageNamed:@"car_1.png"]; use this – Sumit Mundra Sep 22 '14 at 10:07
  • thank you for comment but how can use for example another car picture that has car_1.png as well but that is located in /images/sport-cars/ ? – Matrosov Oleksandr Sep 22 '14 at 10:20
  • you cant,when you see in bundle there is only one image for name car_1.png.its overwriites the same name image with new image. – Sumit Mundra Sep 22 '14 at 11:25
  • @SumitMundra: you are not 100% correct. It is possible if you drag a folder into your project and select Create folder references in Section Added folder. – Deepak Sep 22 '14 at 11:45
  • @deepeak i know this but i talk archive file(ipa)..can you show package contain of archive file and check where that same name 2 image is exit or not – Sumit Mundra Sep 22 '14 at 11:48
  • @SumitMundra i have tried with .ipa file also and it is possible. Try yourself and let me know if you face any difficulty. – Deepak Sep 22 '14 at 12:13
  • i am also check but its not possible. can you give me sample code which you try – Sumit Mundra Sep 22 '14 at 12:59

2 Answers2

3

Ok the problem is that on the simulator and seems on the device as well by some reason when we use @"/Reference Folder Path/image.jpg" it won't work.

Removing this "/" at start of path solve this issue.

You can check this video to see how it works.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • worked like charm :) thank you. One more question is right now if i want to support for both iOS7 and iOS8 can this fix works fine or should we make a version check and provide two different paths? – ravoorinandan Oct 29 '14 at 09:07
  • This bug even occurs on device too. – ravoorinandan Oct 31 '14 at 11:38
  • @ravoorinandan seems yes =) – Matrosov Oleksandr Oct 31 '14 at 12:01
  • @MatrosovAlexander does this change supports iOS7 or should we write a seperate code for iOS7 and iOS8? – ravoorinandan Nov 03 '14 at 04:07
  • @ravoorinandan, I did not remember about that, but seems if you use string without "/" at start it should work for all versions, but you should test it in any case and. But I suppose that you don't need to write separate code just remove "/" and everything should work good. In my case I supported big amount of lines. So I have database that contain about 2000 records with the string that include "/" at start of string. So in my case I just a wrote extension for NSString that delete unneeded "/" for iOS 8. But it works only for my issue, because it is just a path solution not a good practice. – Matrosov Oleksandr Nov 03 '14 at 10:14
0

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension.

So the answer is you have to specify the filename extension for all JPEG.

If this not solve your problem, Try this

Instead of using the imageNamed you can load the image with the methods imageWithContentsOfFile

NSString *imageFile = [NSString stringWithFormat:@"%@/iphone4/%@", [[NSBundle mainBundle] resourcePath], @"1.png"];
UIImage* image = [UIImage imageWithContentsOfFile:imageFile];

NSString *imageFile1 = [NSString stringWithFormat:@"%@/iphone5/%@", [[NSBundle mainBundle] resourcePath], @"1.png"];
UIImage* image1 = [UIImage imageWithContentsOfFile:imageFile1];

NSString *imageFile2 = [NSString stringWithFormat:@"%@/iphone6/%@", [[NSBundle mainBundle] resourcePath], @"1.png"];
UIImage* image2 = [UIImage imageWithContentsOfFile:imageFile2];

I have added 3 folders (with same image name and extension) in my project. I am able to fetch them differently and it work fine

Deepak
  • 1,421
  • 1
  • 16
  • 20
  • thank you for the answer, I just tried to remove extension but it does not work for me, second one as you can see to the picture I've attached there is not an Xcode group folder but resources folder instead that separate files with have the same names. For example if we add two files with the same names to the bundle, the app will not know which to use, because the name of images are the same but it not caused error because they are in different groups/folders. I can use folder 1 and folder 2 with files with the same names car1.png in both folder but they are different files. – Matrosov Oleksandr Sep 22 '14 at 10:27
  • I have edited my code to get same name image from 3 different folder. – Deepak Sep 22 '14 at 11:27
  • sumit, what is the tree structure of your folders? – Deepak Sep 22 '14 at 12:18
  • hello can you sure that you get image by [NSString stringWithFormat:@"%@/iphone4/%@", [[NSBundle mainBundle] resourcePath], @"1.png"]; – Sumit Mundra Sep 23 '14 at 04:44
  • its simple the make the string plz check that you get image on that path or not – Sumit Mundra Sep 23 '14 at 04:50
  • https://www.dropbox.com/sh/iyactuakcqle808/AAAw-ctmtKZqEpveeKqovBpGa?dl=0 check i uplopad some image first image my tree structure and second image show file inside the ipa.when there was only one image how can you get three image..plz explain – Sumit Mundra Sep 23 '14 at 04:52
  • i told you that you need to drag that folder that have same name images in different folder. – Deepak Sep 23 '14 at 05:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61722/discussion-between-deepak-and-sumit-mundra). – Deepak Sep 23 '14 at 05:18
  • When you are dragging the image folder into your project, make sure you select the option that are shown in this image http://i.stack.imgur.com/khfY9.png – Deepak Sep 23 '14 at 05:26