Question

I'm new to cocoa and I have an IKImageBrowserView and that's how I load images to it:

- (IBAction)loadImages:(id)sender
{
NSMutableArray *urls = [[NSMutableArray alloc]init];
int i = 1;
for (i=1; i<55; i++) {
    NSString *photoNumber = [NSString stringWithFormat:@"%i", i];
    NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"Australia"];
    [urlString appendString:photoNumber];
    NSURL* url = [[NSBundle mainBundle] URLForImageResource:urlString];
    [urls addObject:url];
}
[self addImagesWithPaths:urls];
}

    - (void)addAnImageWithPath:(NSString *)path
{
    myImageObject *p;

/* add a path to our temporary array */
p = [[myImageObject alloc] init];
[p setPath:path];
[_importedImages addObject:p];
}

- (void)addImagesWithPath:(NSString *)path recursive:(BOOL)recursive
{
NSInteger i, n;
BOOL dir;

[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir];

if (dir)
{
    NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

    n = [content count];

    // parse the directory content
    for (i=0; i<n; i++)
    {
        if (recursive)
            [self addImagesWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]] recursive:YES];
        else
            [self addAnImageWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
    }
}
else
{
    [self addAnImageWithPath:path];
}
}

/* performed in an independant thread, parse all paths in "paths" and add these paths in our    temporary array */
- (void)addImagesWithPaths:(NSArray *)urls
{
    NSInteger i, n;

n = [urls count];
for ( i= 0; i < n; i++)
{
    NSURL *url = [urls objectAtIndex:i];
    [self addImagesWithPath:[url path] recursive:NO];
}

/* update the datasource in the main thread */
[self performSelectorOnMainThread:@selector(updateDatasource) withObject:nil waitUntilDone:YES];
}

Now my images are loaded by name - @"Australia". That's kind of inconvenient as your images need to have same name and a number. How do I load images with different names from the folder, which has been imported to xcode?

So at the moment I'm loading images by name Australia1, Australia2, Australia3, Australia4... and so on. How do I load images from a bundle folder?

Was it helpful?

Solution

Your data source needs to return items to the image browser view that conform to the IKImageBrowserItem protocol. Your myImageObject class is a good place to start with that.

In that protocol, three methods are required:

For a start, I'd just use the path that you're already giving every myImageObject. You can use that as both the identifier string and the image representation.

Depending on what else you're doing in this app, you may find it advantageous later on, for memory and/or speed reasons, to load each image yourself. If, after profiling, you do come to that conclusion, you can load the image yourself as an NSImage or CGImage, change your representation type appropriately, and return the image as the representation.

As the data source, you'll return the number of items in your _importedImages array, and, when asked for the item at an index, return that item via objectAtIndex:.

More info:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top