how to get a file(image) from a given folder

Discussion in 'iOS Programming' started by tranvutuan, Dec 25, 2011.

  1. tranvutuan macrumors member

    Joined:
    Dec 19, 2011
    #1
    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....
     
  2. KnightWRX, Dec 25, 2011
    Last edited: Dec 25, 2011

    KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    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:.
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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.
     
  4. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #4
    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....

    ----------

    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..
     
  5. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #5
    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.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     
  7. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #7
    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.
     
  8. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #8
    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
     
  9. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #9
    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.
     
  10. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #10
  11. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #11
    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 :)
     
  12. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #12
    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
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    What is your understanding of the purpose of the inDirectory parameter in the [NSBundle pathsForResourcesOfType:inDirectory:] method?
     
  14. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #14
    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
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    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.
     
  16. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #16
    Im sorry it was my typo... Actually what I did was
    Code:
    NSArray *tmp= [NSBundle pathsForResourcesOfType:@"png" inDirectory:@"[B]Resources[/B]/folder1"];
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    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?
     
  18. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #18
    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"
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    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?
     
  20. tranvutuan, Dec 27, 2011
    Last edited: Dec 28, 2011

    tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #20
    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.....
     
  21. tranvutuan thread starter macrumors member

    Joined:
    Dec 19, 2011
    #21
    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....
     
  22. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #22
    ;)
     

Share This Page