Search code examples
nsmutablearraycoverflownsdocumentdirectory

loading images from documents directory into an array


I have some jpg images I am saving to the documents directory from camera shots. I want to find all the jpg images in the documents directory and load them into an array so they can be in a overflow view.

Here is my code to find and sort the jpg images in the documents directory.

   NSString *extension = @"jpg";
        NSFileManager *fileManager = [NSFileManager defaultManager];



        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];  
        NSMutableArray *jpegFiles = [NSMutableArray arrayWithCapacity: [contents count]];

       NSString *filename;
        for (filename in contents)

        {
            if ([[filename pathExtension] isEqualToString:extension]) 
                     {
             [jpegFiles addObject:[UIImage imageNamed:[documentsDirectory stringByAppendingPathComponent:filename]]];


            }
        }

    // Add the coverflow view
    coverflow = [[TKCoverflowView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    coverflow.coverflowDelegate = self;
    coverflow.dataSource = self;
    [self.view addSubview:coverflow];
    [coverflow setNumberOfCovers:6];

- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasBroughtToFront:(int)index{
        NSLog(@"Front %d",index);
}

- (TKCoverflowCoverView*) coverflowView:(TKCoverflowView*)coverflowView coverAtIndex:(int)index{


    TKCoverflowCoverView *cover = [coverflowView dequeueReusableCoverView];

    if(cover == nil){
        // Change the covers size here
        cover = [[TKCoverflowCoverView alloc] initWithFrame:CGRectMake(0, 0, 280, 210)]; // 224
        cover.baseline = 200;

    }
    cover.image = [covers objectAtIndex:index % [covers count]];

    return cover;

}

I have tried everything I can think of and nothing works.

It has been suggested that I load each image by getting the path to the image from the array of pathnames, and use the method imageWithContentsOfFile to load the image.

I have looked this up and did searches for how to do this but with no luck. Could someone show me how to do this.


Solution

  • You should do it as below,

    if ([[filename pathExtension] isEqualToString:extension]) 
    {
         [jpegFiles addObject:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filename]]];
    }