Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Hello everyone
Here is the directory of my images located shown in the right panel of my Xcode:
Code:
-My project
                ---Classes
                ---Resources
                           ----- folder1
                                      ------- [B]img1[/B] (poor quality)
                                      ------- img2
                           ----- folder2
                                      ------- img1 (high quality )
                                      ------- img2
Notice that img1 is also in folder1 and folder2 but they are difference in quality. Now, I have an UIImageView and my goal is set img1 (high quality) as a background of my UIImageView. Here what I coded
Code:
[bigViewOfImg setImage:[UIImage imageNamed:@"Resources/folder1/img1"]];
However, the img is now not shown on UIImageView. .
Others tell me that change the name of 2 imges and code like below
Code:
[bigViewOfImg setImage:[UIImage imageNamed:@"img1.png"]];
For some reason, i dont want to change the name of my imges.Therefore, how can I point to the right folder so that I properly pull out the image which I want to....
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
Have a look at NSBundle's pathForResource:ofType:inDirectory:, along with mainBundle in the same class, then think of how you can use these along with UIImage's initWithContentsOfFile:.

EDIT : of course, if your code already uses UIImage's class method imageNamed:, you might want to use [UIImage imageWithContentsOfFile:] instead of my prior suggestion of initWithContentsOfFile:.
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
Your images will all appear in the same directory after you build your app.

The left panel (you got your left and right backwards in your opening post) displays things as being within folders only for convenience... To help keep your app files organized. In reality they will all be heaped into a single bundle with no subdirectories on the iPhone.

Thus, do not name multiple files the same thing, regardless of how you've organized them in Xcode.

You said the images were identical, except for quality. By this do you mean retina quality vs. pre-retina quality? If so, add "@2x" after the name, but before the extension, of the higher quality, retina, image, so that Xcode will know that the high quality image should be displayed on the iPhone 4 and 4S while the low quality one should be displayed on the 3GS.
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Have a look at NSBundle's pathForResource:ofType:inDirectory:, along with mainBundle in the same class, then think of how you can use these along with UIImage's initWithContentsOfFile:.

EDIT : of course, if your code already uses UIImage's class method imageNamed:, you might want to use [UIImage imageWithContentsOfFile:] instead of my prior suggestion of initWithContentsOfFile:.

I just gave it a tried like below
Code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"img1" ofType:@"png" inDirectory:@"[U]Resource/folder1[/U]" ];
However, when I debugged by printing the filepath on the console, it is
filePath is (null)
I also tried like below

Code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"img1" ofType:@"png" inDirectory:@"[U]folder1[/U]" ];
But it does not work either....

----------

Your images will all appear in the same directory after you build your app.

The left panel (you got your left and right backwards in your opening post) displays things as being within folders only for convenience... To help keep your app files organized. In reality they will all be heaped into a single bundle with no subdirectories on the iPhone.

Thus, do not name multiple files the same thing, regardless of how you've organized them in Xcode.

You said the images were identical, except for quality. By this do you mean retina quality vs. pre-retina quality? If so, add "@2x" after the name, but before the extension, of the higher quality, retina, image, so that Xcode will know that the high quality image should be displayed on the iPhone 4 and 4S while the low quality one should be displayed on the 3GS.

They are same imges but one is small size of 70X70 and the other is big size of 200X200. So if I add"@2x", which one should be added, the small or the large one..
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
But it does not work either....

Nope, I checked after what ArtofWarfare posted and indeed, Xcode builds IPA bundles as one big mess. Just go to your build folder and do a show package on your bundle and you'll see everything is lumped into the root folder of the package.

They are same imges but one is small size of 70X70 and the other is big size of 200X200. So if I add"@2x", which one should be added, the small or the large one..

His trick was for Retina vs non-Retina display. It seems to me you want to do thumbnail previews and full display images. You'll have to work out yourself how to send 2 different strings instead of folder names to your classes.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Nope, I checked after what ArtofWarfare posted and indeed, Xcode builds IPA bundles as one big mess. Just go to your build folder and do a show package on your bundle and you'll see everything is lumped into the root folder of the package.

Yes, by default it's one big folder. It is, however, possible to create and use subfolders within it but it is rarely done because hardly anybody spends time in the IPA Package Contents. It is more important to make sure you have things organized within Xcode, where you do spend your time, and that is done via Groups.
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
Yes, by default it's one big folder. It is, however, possible to create and use subfolders within it but it is rarely done because hardly anybody spends time in the IPA Package Contents. It is more important to make sure you have things organized within Xcode, where you do spend your time, and that is done via Groups.

Well, seems for the OP it would be a good solution if his code already relies on the sub-folders. If it is doable, he could then apply the NSBundle method I gave him which would be simpler than re-factor his code for different image names I guess.
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Nope, I checked after what ArtofWarfare posted and indeed, Xcode builds IPA bundles as one big mess. Just go to your build folder and do a show package on your bundle and you'll see everything is lumped into the root folder of the package.



His trick was for Retina vs non-Retina display. It seems to me you want to do thumbnail previews and full display images. You'll have to work out yourself how to send 2 different strings instead of folder names to your classes.

yeah I want to do thumbnail and fully display images. The images are same name but different folders. That is why I have to figure out how to get their directory properly so that I can point to the right image
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
yeah I want to do thumbnail and fully display images. The images are same name but different folders. That is why I have to figure out how to get their directory properly so that I can point to the right image

I found something that explains how to do what Dejo said :

http://www.iphonedevsdk.com/forum/iphone-sdk-development/6457-xcode-folder-directories.html

This guy seems to want to do the same thing you are doing. After that, the NSBundle trick should work.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
You can copy resources into a Bundle via Xcode, then you can look for items in a specific subfolder. this is done by opening xcode.
Clicking on build phases (where you add frameworks).
There you can add a Build Phase, where you can set it all up :)
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
I found something that explains how to do what Dejo said :

http://www.iphonedevsdk.com/forum/iphone-sdk-development/6457-xcode-folder-directories.html

This guy seems to want to do the same thing you are doing. After that, the NSBundle trick should work.

Now, I can pull the img from my folder1 by
Code:
[bigViewOfImg setImage:[UIImage imageNamed:@"folder1/img1.png"]];


My next task is to print all path of my imges ( img1 + img2 )from my folder1. To do so, I do
Code:
NSArray *tmp    =   [NSBundle pathsForResourcesOfType:@"png" inDirectory:[[NSBundle mainBundle] bundlePath]];
and I got so many result and what is not I expected. In the documentation.. they define
Code:
[[NSBundle mainBundle] bundlePath] = the top level of bundle
but my directory is a sub-directory...
Please advice me on this issue..... Thanks a lot
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
What is your understanding of the purpose of the inDirectory parameter in the [NSBundle pathsForResourcesOfType:inDirectory:] method?
Following to the definition at http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html and What I have to derived by coding to test it like
Code:
NSArray *tmp= [NSBundle pathsForResourcesOfType:@"png" inDirectory:@"[B]Resource/folder1[/B]"];
    for (id obj in tmp )
        NSLog(@"%@",tmp); // [B]it give me null ---->NSArray is empty----->path not exist somehow[/B]
Therefore, I think inDirectory must be a directory in which myProject.app is located.
If I have made a mistake, pls correct me. Thanks
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
NSArray *tmp= [NSBundle pathsForResourcesOfType:@"png" inDirectory:@"[B]Resource/folder1[/B]"];

Accuracy is very important in programming. Resource/folder1 is not the same as Resources/folder1, which is the folder name you claimed to have in your project in your original post.
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Accuracy is very important in programming. Resource/folder1 is not the same as Resources/folder1, which is the folder name you claimed to have in your project in your original post.
Im sorry it was my typo... Actually what I did was
Code:
NSArray *tmp= [NSBundle pathsForResourcesOfType:@"png" inDirectory:@"[B]Resources[/B]/folder1"];
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Im sorry it was my typo...

When you include code-snippets, whenever possible you should copy-and-paste them from the actual Xcode project. That way, the code we are attempting to help you debug is the same code that you have written.

Code:
NSArray *tmp= [NSBundle pathsForResourcesOfType:@"png" inDirectory:@"[B]Resources[/B]/folder1"];

So, now that you've given us the correct code, what is it doing? Or not doing?
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
When you include code-snippets, whenever possible you should copy-and-paste them from the actual Xcode project. That way, the code we are attempting to help you debug is the same code that you have written.



So, now that you've given us the correct code, what is it doing? Or not doing?
I am sorry to confuse you then...Given a right path :Resources/folders, I would like to get all full pathnames for all files with the an extension of "png"
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I am sorry to confuse you then...Given a right path :Resources/folders, I would like to get all full pathnames for all files with the an extension of "png"

Yes, I already understand what it is you're trying to do. But what does the code you've provided actually do, or not do? That is, what is the value of tmp after you run that line of code? Is it nil? If it is, what have you done in terms of debugging to determine why it's nil? If it isn't nil, what is does it contain? And does what it contain match up with what you expect it to contain? If not, ...

In other words, what basic debugging have you done?
 

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Yes, I already understand what it is you're trying to do. But what does the code you've provided actually do, or not do? That is, what is the value of tmp after you run that line of code? Is it nil? If it is, what have you done in terms of debugging to determine why it's nil? If it isn't nil, what is does it contain? And does what it contain match up with what you expect it to contain? If not, ...

In other words, what basic debugging have you done?
The debugger gave me nothing when I tried to print the contents ( all path type of string of png files ).That is why I guess my NSArray is nil then...
Im not so sure about the method which I invoked,I think I used the wrong one because the inDirectory parameter should be the top level directory of a bundle. However, I was passing a sub-path as an argument.....
 
Last edited:

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Yes, I already understand what it is you're trying to do. But what does the code you've provided actually do, or not do? That is, what is the value of tmp after you run that line of code? Is it nil? If it is, what have you done in terms of debugging to determine why it's nil? If it isn't nil, what is does it contain? And does what it contain match up with what you expect it to contain? If not, ...

In other words, what basic debugging have you done?
Hey I know why my tmp is NIL now...The reason is I have not initialize my bundle yet...
When I made a small change of my code to
Code:
NSArray *tmp= [[B][NSBundle mainBundle] [/B]pathsForResourcesOfType:@"png" inDirectory:@"Resources/folder1"];
Then I can pull out all of the path of png file...Thanks for your help after all....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.