2

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!

1 Answer 1

3

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];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks that makes much more sense.... It is still a black screen when I run the app though. How do I tell if the images are getting loaded? Thanks again!
Run the code with the debugger and step through the showPics method. Also make sure you add your image view to the view controller's view.
THis was a great fix. The other issue was that I had a group instead of a reference to the folder. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.