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

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,761
8,463
A sea of green
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];
[COLOR="Red"]    [imagesArray addObject:[fileManager contentsOfDirectoryAtPath:imageResourcesFolderPath error:nil]];
[/COLOR]    
    [paletteView setDataSource:imagesArray];
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.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
contentsOfDirectoryAtPath:error: returns an NSArray, I misread the Apple Documentation and thought it returned something else. What method do you suggest?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
contentsOfDirectoryAtPath:error: returns an NSArray, I misread the Apple Documentation and thought it returned something else. What method do you suggest?

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).
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

I've no idea. What does the IKImageBrowserView documentation say it needs?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

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
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,761
8,463
A sea of green
Are you trying to initialize a single image with a folder? How does that work?

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.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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.

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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Code:
    NSBundle *resourcesBundle = [[NSBundle mainBundle]init];

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];
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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
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.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
So I add the images to an NSMutable Array then set IKImageBrowserView's dataSouce: to that NSMutableArray full of images?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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

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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.