Having trouble with the following bit of code:
int count = [imageArray count];
for (int i = 0; i <= count ; i++)
{
UIImage *currentImage = [imageArray objectAtIndex: i];
UIImage *nextImage = [imageArray objectAtIndex: i +1];
self.imageview.image = [imageArray objectAtIndex: i];
[self.view addSubview:self.imageview];
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
crossFade.fromValue = (__bridge id)(currentImage.CGImage);
crossFade.toValue = (__bridge id)(nextImage.CGImage);
[self.imageview.layer addAnimation:crossFade forKey:@"animateContents"];
self.imageview.image = nextImage;
};
Very new to iOS coding so any help would be appreciated, just need to know how to stop the error.
The issue is due to this piece of code:
and the condition you used :
i <= countSuppose your array contains 6 objects. Then the loop will run from 0 – 6. Array index starts from 0, so the 6th elements index is 5. If you try to fetch the objectAtIndex:6 a crash will occur.
Like wise you are taking the image at i+1 if the i is 5, then it’ll try to fetch i+1 th means 6th element. Probably a crash.
Change your method like: