PDA

View Full Version : Populating NSMutableArray




Blakeasd
May 5, 2011, 03:16 PM
Hello,
I am having some trouble populating NSMutableArray with images. This NSMutableArray will be my datasource for an IKImageBrowserView.
Here is my 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
May 5, 2011, 03:19 PM
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
May 5, 2011, 03:37 PM
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];


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
May 5, 2011, 03:39 PM
contentsOfDirectoryAtPath:error: returns an NSArray, I misread the Apple Documentation and thought it returned something else. What method do you suggest?

robbieduncan
May 5, 2011, 03:45 PM
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
May 5, 2011, 05:01 PM
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
May 5, 2011, 05:03 PM
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
May 5, 2011, 05:05 PM
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
May 5, 2011, 05:36 PM
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)


-(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

Sydde
May 5, 2011, 06:13 PM
Are you trying to initialize a single image with a folder? How does that work?

chown33
May 5, 2011, 06:24 PM
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/mac/#documentation/GraphicsImaging/Reference/IKImageBrowserView/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
May 5, 2011, 06:30 PM
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
May 5, 2011, 07:09 PM
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:

NSBundle *resourcesBundle = [NSBundle mainBundle];

Blakeasd
May 5, 2011, 07:28 PM
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
May 5, 2011, 08:26 PM
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
May 5, 2011, 08:46 PM
So I add the images to an NSMutable Array then set IKImageBrowserView's dataSouce: to that NSMutableArray full of images?

jiminaus
May 5, 2011, 08:59 PM
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.