Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 5, 2011, 03:16 PM   #1
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
Populating NSMutableArray

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.
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 03:19 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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?
robbieduncan is offline   0 Reply With Quote
Old May 5, 2011, 03:37 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Blakeasd View Post
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];
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.
chown33 is offline   0 Reply With Quote
Old May 5, 2011, 03:39 PM   #4
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
contentsOfDirectoryAtPath:error: returns an NSArray, I misread the Apple Documentation and thought it returned something else. What method do you suggest?
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 03:45 PM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Blakeasd View Post
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).
robbieduncan is offline   0 Reply With Quote
Old May 5, 2011, 05:01 PM   #6
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
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
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 05:03 PM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Blakeasd View Post
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?
robbieduncan is offline   0 Reply With Quote
Old May 5, 2011, 05:05 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by Blakeasd View Post
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 initWithContentsOfFileNSString *), 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
lee1210 is offline   0 Reply With Quote
Old May 5, 2011, 05:36 PM   #9
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
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
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 06:13 PM   #10
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Are you trying to initialize a single image with a folder? How does that work?
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 5, 2011, 06:24 PM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Sydde View Post
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/m...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.
chown33 is offline   0 Reply With Quote
Old May 5, 2011, 06:30 PM   #12
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by chown33 View Post
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 5, 2011, 07:09 PM   #13
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Blakeasd View Post
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];
jiminaus is offline   0 Reply With Quote
Old May 5, 2011, 07:28 PM   #14
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
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
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 08:26 PM   #15
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by Blakeasd View Post
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 5, 2011, 08:46 PM   #16
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
So I add the images to an NSMutable Array then set IKImageBrowserView's dataSouce: to that NSMutableArray full of images?
Blakeasd is offline   0 Reply With Quote
Old May 5, 2011, 08:59 PM   #17
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Blakeasd View Post
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 by jiminaus; May 5, 2011 at 09:23 PM.
jiminaus is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Notification Center - ALL tab not populating when phone is locked blutrane iOS 7 2 Sep 23, 2013 10:30 AM
Populating TableView Mvkoe iPhone/iPad Programming 5 Dec 1, 2012 04:34 PM
UITableView populating at cell LARRYSE iPhone/iPad Programming 1 Sep 12, 2012 11:28 PM
help populating a uitableview LARRYSE iPhone/iPad Programming 17 Aug 30, 2012 08:37 AM
UITableView populating help LARRYSE iPhone Tips, Help and Troubleshooting 1 Aug 22, 2012 01:47 PM

Forum Jump

All times are GMT -5. The time now is 08:37 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC