Hi,
I'm rather new to iPhone programming, and I can't get this figured out, in what should be a simple test program.
I have a main view that contains two buttons and a UIImageView. On viewDidLoad, I load into the UIImageView the first of six images that are in my resource folder:
This works fine.
imgShown is defined here:
IBOutlet UIImageView *imgShown;
and it is connected to the UIImageView on the screen that I placed using Interface Builder.
If I click the "Load Next Image" button, the following code executes, and I am able to successfully cycle through the images on the iPhone's screen:
However, I have another button, called "Load Previous Image," and the method loadPreviousImage is **identical** to loadNextImage above, except for the first line, which says
currentImage--;
a decrement instead of an increment. loadPreviousImage *never* works, it crashes the program.
The debug window says:
Program received signal: “EXC_BAD_ACCESS”.
and it seems the crash happens on the following line:
[imgShown setImage:img];
More information:
When I added the following line to the beginning of loadNextImage:
currentImage++
if (currentImage == 6) currentImage = 0;
I expected to be able to cycle through the images, going back to the first after seeing the last when pressing "Load Next Image", but I get the same crash and error when it tries to load image index 0 again.
Also, if loadNextImage ignores the currentImage integer and just says:
NSString *strNextImage = [[self.imgNamesArray objectAtIndex:4] stringByAppendingFormat
".png"];
...see? I'm just placing image index number 4. *The first time I press the button, it loads the proper image, index #4, and the next time, I get the error.
It seems that the error is generated whenever I try to load an image that was previously loaded. *If I try to repeat a previously-shown image, then error.
Can anyone help?
Thanks,
/S
I'm rather new to iPhone programming, and I can't get this figured out, in what should be a simple test program.
I have a main view that contains two buttons and a UIImageView. On viewDidLoad, I load into the UIImageView the first of six images that are in my resource folder:
Code:
- (void)viewDidLoad {
self.imgNamesArray = [NSArray arrayWithObjects:@"Sima_bouncy", @"DannySpikes", @"DanSima", @"DaddyDanny", @"MikeHorse", @"SashaTeeth", nil];
NSString *strFirstImage = [[imgNamesArray objectAtIndex:0] stringByAppendingFormat:@".png"];
UIImage *img = [UIImage imageNamed:strFirstImage];
if (img != nil) {
[imgShown setImage:img];
[img release];
}
[strFirstImage release];
[super viewDidLoad];
}
This works fine.
imgShown is defined here:
IBOutlet UIImageView *imgShown;
and it is connected to the UIImageView on the screen that I placed using Interface Builder.
If I click the "Load Next Image" button, the following code executes, and I am able to successfully cycle through the images on the iPhone's screen:
Code:
-(IBAction)loadNextImage:(id)sender {
currentImage++;
NSString *strNextImage = [[self.imgNamesArray objectAtIndex:currentImage] stringByAppendingFormat:@".png"];
UIImage *img = [UIImage imageNamed:strNextImage];
if (img != nil) {
[imgShown setImage:img];
[img release];
}
[strNextImage release];
}
However, I have another button, called "Load Previous Image," and the method loadPreviousImage is **identical** to loadNextImage above, except for the first line, which says
currentImage--;
a decrement instead of an increment. loadPreviousImage *never* works, it crashes the program.
The debug window says:
Program received signal: “EXC_BAD_ACCESS”.
and it seems the crash happens on the following line:
[imgShown setImage:img];
More information:
When I added the following line to the beginning of loadNextImage:
currentImage++
if (currentImage == 6) currentImage = 0;
I expected to be able to cycle through the images, going back to the first after seeing the last when pressing "Load Next Image", but I get the same crash and error when it tries to load image index 0 again.
Also, if loadNextImage ignores the currentImage integer and just says:
NSString *strNextImage = [[self.imgNamesArray objectAtIndex:4] stringByAppendingFormat
...see? I'm just placing image index number 4. *The first time I press the button, it loads the proper image, index #4, and the next time, I get the error.
It seems that the error is generated whenever I try to load an image that was previously loaded. *If I try to repeat a previously-shown image, then error.
Can anyone help?
Thanks,
/S