user1344851
user1344851

Reputation:

How to get path for images objective-C

I want to call images from different folder, would you please help me to implement this;

I have this structure for my folders :

Folder1

  file1

     image1.jpg

  file2

     image2.jpg

  file3

     image3.jpg

Folder2 ....

inside of each file%d I have one image%.jpg

I want to call my images in loop,

 for (i = 1; i > image; i++)
{
    NSString *image = [NSString stringWithFormat:@"image%d.jpg", i];

But my question is how to enter to the folders and get images

can I have something like this :

 stringWithFormat:@"Folder%d/File%d/image%d" ofType:@"jpg"

what is the best way?

Thanks in advance!

Upvotes: 1

Views: 4348

Answers (3)

atxe
atxe

Reputation: 5079

Iterate folders and images appending the image path to the library path:

NSString * libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];

for (NSUInteger f = 1; f <= numFolders; i++) {

    for (NSUInteger i = 1; i <= numImages; i++) {

        // Create the image path
        NSString * imagePath = [NSString stringWithFormat:@"%@/Folder%d/File%d/image%d.jpg", libraryPath, f, i, i];

        // Check if file exists
        if ( [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            UIImage * image = [UIImage imageWithContentsOfFile:imagePath];

            // Use image...
        }
        else {
            NSLog(@"Image not found.");
        }
    }
}

Upvotes: 0

Alex
Alex

Reputation: 5119

Right, this is how I would do it.

for (f = 1; f <= numberOfFolders; i++) {
    for (i = 1; i <= numberOfImages; i++) {
        NSString* file = [NSString stringWithFormat: @".../Folder%d/File%d/image%d.jpg", f, i, i];
        NSImage* image = [NSImage imageNamed: file];

        // Do something with the image
    }
}

A note: I don't know where Folder1/Folder2... etc are, so I used '.../' on the code. Be sure to fill that part appropriately.

Upvotes: 0

Gordon Dove
Gordon Dove

Reputation: 2577

Here are a few useful code snippets that work for me.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *mapDirPath = [documentsDirectory stringByAppendingPathComponent:@"/MapCache"];

// Creating a directory in the documents directory

NSError* error;
if (![[NSFileManager defaultManager] fileExistsAtPath:mapDirPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:mapDirPath withIntermediateDirectories:NO attributes:nil error:&error];

// loading an image

NSString *filePath = [self.localImagePath stringByAppendingPathComponent: key];
if ( [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    UIImage* image = [UIImage imageWithContentsOfFile:filePath];
    return image;
}

// Writing an image

[[NSFileManager defaultManager] createFileAtPath:filePath contents:remoteData attributes:nil];

Hope that helps

Of course, if you're reading images from your bundle, you want this instead [[NSBundle mainBundle] pathForResource: @"filename" ofType:@"png" directory: @"directory1"]

Upvotes: 2

Related Questions