I've got this code trying to run a simple set of images in a cycle. All I have in the app is one UIImageView declared in my View Controller's .h file:
@property (strong, nonatomic) IBOutlet UIImageView *imageDisplay;
And the following in my .m file's viewDidLoad method:
NSMutableArray *imageView = [[NSMutableArray alloc] init];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim1.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim2.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim3.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim4.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim5.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim6.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"EyeAnim7.png"]]];
imageDisplay.animationImages = imageView;
imageDisplay.animationDuration = 0.25;
imageDisplay.animationRepeatCount = 50;
[imageDisplay startAnimating];
The code seems to be crashing on the "imageDisplay.animationImages" line, as if I create the UIImageView, create its getter and setter, and build, it's fine until I uncomment that line. If I do uncomment it, it keeps giving me the error until I delete the UIImageView and create a new one.
Not too sure what's happening, any help appreciated!
animationImages array MUST contain only UIImage objects. Your array contains UIImageView objects.
Also your code is unsafe - if one of the resources will not exist app will crash (trying to add nil object to the mutable array). This will be much safer:
#define kNumberOfImages 7
NSMutableArray *imageView = [[NSMutableArray alloc] init];
for(NSUInteger i = 1; i <= kNumberOfImages; i++) {
UIImage *anImage = [UIImage imageNamed:[NSString stringWithFormat:@"EyeAnim%d", i]];
if(anImage) {
[imageView addObject:anImage];
}
}
self.imageDisplay.animationImages = imageView;
self.imageDisplay.animationDuration = 0.25;
self.imageDisplay.animationRepeatCount = 50;
[self.imageDisplay startAnimating];
I am very new to objective-c and I have been getting this error also, but for a different reason. I just wanted to post my solution for anyone else who may be struggling.
So basically I have a custom class called ImagesDetailViewController which inherits from UIViewController and has an image property.
@interface ImagesDetailViewController : UIViewController
@property (strong, nonatomic) UIImage *image;
@end
I then connected my class to my UIImageView on my storyboard like so
@interface ImagesDetailViewController ()
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@end
In my viewDidLoad method I was trying to set the image for my image view like this and getting the error mentioned above (my image variable gets initialised in a prepareForSegue method)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.imageView setImage:self.image];
}
So I was stumped as I bet you are all too. The problem had to do with the storyboard. Clicking on my UIImageView and then navigating to the Connections Inspector, I had somehow created 2 referencing outlets (oops...) and one was pointing to a variable called image. So when the program was running [self.imageView setImage:self.image] self.image was actually an instance of UIImageView instead of UIImage.