Siriss
Siriss

Reputation: 3767

Loading array from folder of images - xcode

I am having some trouble loading images from a file into an array. I have used a combination of questions I have found on here, and am out of ideas.... I am new to objective-c and rusty on the rest.

My viewDidLoad simply calls my showPics method, and for testing sake I have the _imgView just show the image at position 1 in the array.

It could very well be a problem with the way I am showing the images as well. I have a ViewController and one ImageView (titled: imgView) in my Storyboard.

here is my showPics method:

-(void)showPics
{
    NSArray *PhotoArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"Otter_Images"];
    NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:PhotoArray.count];
    for (NSString* path in PhotoArray)
    {
        [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
    }
    UIImage *currentPic = _imgView.image;
    int i = -1;

    if (currentPic != nil && [PhotoArray containsObject:currentPic]) {
        i = [PhotoArray indexOfObject:currentPic];
    }

    i++;
    if(i < PhotoArray.count)
        _imgView.image= [PhotoArray objectAtIndex:1];

}

Here is my viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showPics];
}

Here is my ViewController.h

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

Please let me know if you need anything else and thank you in advance!

Upvotes: 2

Views: 4516

Answers (1)

rmaddy
rmaddy

Reputation: 318774

In your showPics method, other than the initial 'for-loop', all of your references to PhotoArray should instead be references to imgQueue. PhotoArray is a list of pathnames. imgQueue is the array of actual UIImage objects.

-(void)showPics {
    NSArray *PhotoArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"Otter_Images"];
    NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:PhotoArray.count];
    for (NSString* path in PhotoArray) {
        [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
    }

    UIImage *currentPic = _imgView.image;
    int i = -1;

    if (currentPic != nil && [imgQueue containsObject:currentPic]) {
        i = [imgQueue indexOfObject:currentPic];
    }

    i++;
    if(i < imgQueue.count) {
        _imgView.image = [imgQueue objectAtIndex:1];
    }
}

Upvotes: 3

Related Questions