Populating NSMutableArray

Discussion in 'Mac Programming' started by Blakeasd, May 5, 2011.

  1. macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am having some trouble populating NSMutableArray with images. This NSMutableArray will be my datasource for an IKImageBrowserView.
    Here is my code:
    Code:
    
     NSBundle *resourcesBundle = [[NSBundle mainBundle]init];
        NSString *resourcesPathToApp = [resourcesBundle resourcePath];
        NSString *resourcesPathToAppExtraInfo = @"/ImageResourceFolder";
        NSString *imageResourcesFolderPath = [resourcesPathToApp stringByAppendingString:resourcesPathToAppExtraInfo];
        
        NSFileManager *fileManager = [[NSFileManager alloc]init];
        NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
        [imagesArray addObject:[fileManager contentsOfDirectoryAtPath:imageResourcesFolderPath error:nil]];
        
        [paletteView setDataSource:imagesArray];
    
    
    NOTE: paletteView is the IKImageBrowserView I declared in the .h file.

    Any ideas as to why this not working. I don't get any errors or warnings, the problem is my IKImageBrowserView remains blank.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What does contentsOfDirectoryAtPath:error: return? If you add that to an NSMutableArray does that actually get you what you want? As an even less subtle hint does adding an NSArray to a NSMutableArray result in a NSMutableArray with all the objects from the NSArray or containing the NSArray which, in turn, contains the objects?
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    The red-hilited line doesn't populate imagesArray with images. It simply adds the object returned by contentsOfDirectoryAtPath to imagesArray.

    What is the object returned by contentsOfDirectoryAtPath?
    "An array of NSString objects identifying the directories and files (including symbolic links) contained in path."
    That's quoted directly from the Return Value heading of the class reference doc for NSFileManager's contentsOfDirectoryAtPath method.

    You have to process each item in the returned array as a pathname component, and load an image from the file referred to by each pathname. That image is what you put into imagesArray.
     
  4. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #4
    contentsOfDirectoryAtPath:error: returns an NSArray, I misread the Apple Documentation and thought it returned something else. What method do you suggest?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    I suggest you:

    1) Look at what is in the NSArray returned. Is it actually what you want? It's obviously not going to be an array of NSImages. But is it even an array of absolute paths?

    2) Look at the NSMutableArray documentation for a method that will add objects from another array (the documentation should be your first port of call anyway).
     
  6. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    I looked at the contentsOfDirectoryAtPath:error: method and Apple states "An array of NSString objects identifying the directories and files (including symbolic links) contained in path." So my question is, will the IKImageBrowserView use this string and display the image? If not what is the solution to this problem?
    Thanks
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    I've no idea. What does the IKImageBrowserView documentation say it needs?
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    An NSString containing a path is not an NSImage.

    NSImage has an initializer initWithContentsOfFile:(NSString *), so that is a step in the right direction for getting an NSImage for a path. You'll need to loop over your NSArray of NSStrings and get an NSImage for each, and then add that NSImage to your NSMutableArray. There are some other issues you'll need to deal with in terms of whether or not an NSString is a path to an image or a subdirectory, traversing subdirectories, etc.

    -Lee
     
  9. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    Ok, I have tried some of the changes everyone has suggested(I think I followed your instructions correctly)Here is my new code: (I haven't tried looping it yet)

    Code:
    -(void)awakeFromNib{
            
        NSBundle *resourcesBundle = [[NSBundle mainBundle]init];
        NSString *resourcesPathToApp = [resourcesBundle resourcePath];
        NSString *resourcesPathToAppExtraInfo = @"/ImageResourceFolder";
        NSString *imageResourcesFolderPath = [resourcesPathToApp stringByAppendingString:resourcesPathToAppExtraInfo];
        
        NSFileManager *fileManager = [[NSFileManager alloc]init];
        NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
        [imagesArray addObject:[fileManager contentsOfDirectoryAtPath:imageResourcesFolderPath error:nil]];
      
        NSImage *image = [[NSImage alloc]initWithContentsOfFile:imageResourcesFolderPath];
        [paletteView setDataSource:image];
        
       }
    
    The code doesn't work though :( , what am I doing wrong and how can I fix it? Once again, I don't get any errors or warnings it's just that IKImageBrowserView doesn't populate with images.
    Thanks For Everyone's Help So Far
     
  10. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #10
    Are you trying to initialize a single image with a folder? How does that work?
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    Not only that, but NSImage isn't an IKImageBrowserDataSource.

    The data source of an IKImageBrowserView must implement IKImageBrowserDataSource. As it says in the class reference:
    http://developer.apple.com/library/...BrowserView/IKImageBrowserView_Reference.html

    There are two required methods in the IKImageBrowserDataSource, so a plain NSArray or NSMutableArray isn't sufficient, either. There needs to be an adapter class of some kind.

    The OP should probably look at the sample code referenced from the IKImageBrowserView class reference doc.
     
  12. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #12
    Well, theoretically, no. You could write datasource methods as a Category to NSMutableArray, so subclassing is not absolutely necessary. Kind of a crazy notion, but it could be kind of handy if it works (every array you use would have datasource potential). Still, some amount of coding would be required.
     
  13. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    Hopefully it's harmless to re-init the object returned by [NSBundle mainBundle]. But just in case it isn't (and because it's wrong), this line should be simply:
    Code:
        NSBundle *resourcesBundle = [NSBundle mainBundle];
    
     
  14. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #14
    I have several images in a folder, I know how to access the folder, I just need to display these images in an IKImageBrowserView, so I guess a better question would be:
    How do I display a folder of images in an IKImageBrowserView?
    Thanks
     
  15. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #15
    You have to put them into the array (NSMutableArray) one at a time. Computers are pretty stupid, they do not understand simple things, we have to tell them step by step what to do.
     
  16. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #16
    So I add the images to an NSMutable Array then set IKImageBrowserView's dataSouce: to that NSMutableArray full of images?
     
  17. jiminaus, May 5, 2011
    Last edited: May 5, 2011

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #17
    You're really going to want to study the ImageBrowser sample code. It doesn't do exactly what you want, in that you won't be able to just copy'n'paste that code into your's. But it does contain all the code you need.

    You will be able to just pass on the paths to IKImageBrowserView and it will load the images for you. You won't have to load the images yourself. But you can't just give an array of strings to IKImageBrowserView.

    Your controller will act as the immediate data source and store the NSArray as ivar and/or property. It will respond to the numberOfItemsInImageBrowser: and imageBrowser:itemAtIndex: messages.

    But it won't pass on NSString objects directly to IKImageBrowserView. Instead it will wrap the string in a custom class so IKImageBrowserView which implementes the required IKImageBrowserItem protocol. This custom class will tell IKImageBrowserView that it's being given paths to images and not images themselves. See the myImageObject class in the ImageBrowser sample code for how the wrapper class could be implemented.

    EDIT: You can't set the dataSource of an IKImageBrowserView to be an NSArray of anything (unless you implement a category as sydde thought of). The data source must at least respond to the required messages of the IKImageBrowserDataSource informal protocol.
     

Share This Page