5

I have function, which received 2 arguments - image and number. Function below throws EXC BAD ACCESS on line "imgData = UIImageJPEGRepresentation(image, 1.0);". I don't know why :-( Any ideas?

- (void)saveImages:(UIImage*)image row:(int)row {
  if (image != nil) {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path1 = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithFormat:@"%i.JPEG", row]];
    NSString* path2 = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithFormat:@"%is.JPEG", row]];

    NSData* imgData;
    imgData = UIImageJPEGRepresentation(image, 1.0);
    [imgData writeToFile:path1 atomically:YES];

    [self rotateIcon:image];
    image = [self imageWithImage:image];
    NSData* smalImgData = UIImageJPEGRepresentation(image, 1.0);
    [smalImgData writeToFile:path2 atomically:YES];
  }
}
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Michal Jurník
  • 820
  • 1
  • 8
  • 23
  • 1
    `image` is likely pointing at an over released object. – Joe Dec 15 '11 at 16:07
  • 1
    Hi, thanks for answer. You mean here image = [self imageWithImage:image]; ? – Michal Jurník Dec 15 '11 at 16:30
  • 1
    @MichalJuriJurník: That's after the crash, so that hasn't happened yet. `image` is one of the parameters you passed to `UIImageJPEGRepresentation`; if it points to a dead object, then that will cause a crash when `UIImageJPEGRepresentation` tries to use that dead object. – Peter Hosey Dec 15 '11 at 19:38
  • @PeterHosey Ok, so recipe how to get an image could be send a path to function (saveImages) and load UIImage from the path? – Michal Jurník Dec 16 '11 at 15:27
  • THX guys, It seems to be solved. :-) I simply created a new slot, which contains Image. You have right. When I called dismissModalViewControllerAnimated in controller, image (which is in another controller) points to a dead object. Thx a lot guys! ;-) – Michal Jurník Dec 16 '11 at 15:50

0 Answers0