views:

68

answers:

1

Hi all, I have a problem with my application. It crashes after few times of working with images and I have no idea why. I have a TabBar, in one item of TabBat I have a navigation controller. In the root controller of that navigation I have a two buttons. One button is to get an image from camera or photo library, the second button is to get an image from saved images in the app. With selected image I go to next view controller (SelectImageViewController).

With the first button I use that code:

- (void) showImagePickerWithSourceType:(NSInteger) sourceType
{
     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
     picker.delegate = self;
     picker.sourceType = sourceType;
     picker.allowsEditing = NO;
     [self presentModalViewController:picker animated:YES];
     [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     UIImage *selectedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerEditedImage"];

     if (selectedImage == nil)
         selectedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

     [[picker parentViewController] dismissModalViewControllerAnimated:YES];

     SelectImageViewController *selectImageViewController = [[[SelectImageViewController alloc] initWithImage:selectedImage] autorelease];
     [self.navigationController pushViewController:selectImageViewController animated:YES];
}

With the second button I use:

DocumentsViewController *documentsViewController = [[[DocumentsViewController alloc] init] autorelease];
[self.navigationController pushViewController:documentsViewController animated:YES];

DocumentsViewController is a TableViewController with list of images, when I select one image than I go to the SelectImageViewController.

In SelectImageViewController I make some image processing and then go to next view controller where I make another image processing.

Here is some code from SelectImageViewController:

- (id) initWithImage:(UIImage *) image
{
    self = [super init];
    if (!self) return nil;

    self.originalImage = image;

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

   [NSThread detachNewThreadSelector:@selector(processImage) toTarget:self withObject:nil];
 }

- (void) processImage
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   self.originalImage = [ImageHelper scaleAndRotateImage:self.originalImage toSize:CGSizeMake(1500, 1500)];
   self.processedImage = [ImageHelper scaleImage:self.originalImage maxWidth:320 maxHeight:367];

   [self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO];

   [pool release];
}

- (void) finish
{
   imageView = [[UIImageView alloc] initWithImage:self.processedImage];
   imageView.center = self.view.center;

   [self.view addSubview:imageView];
   [self.view sendSubviewToBack:imageView];

   [NSThread detachNewThreadSelector:@selector(processImage2) toTarget:self withObject:nil];
}

- (void) processImage2
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSArray *corners = [OpenCV processImage:self.processedImage];

  [self performSelectorOnMainThread:@selector(finish2) withObject:nil waitUntilDone:NO];

  [pool release];
}

In the SelectImageViewController I have one button and when I click it I process the original image and then go to the next view controller. In the last view controller in viewDidLoad method I process one more time the image.

I know that all method for image processing take a lot of memory and some time (especially that my original image which I use often is not scaled), but the application also crashes when I only select an image from photo library and go to SelectImageViewController. Maybe I shouldn't use a navigation controller? Has anybody an idea what can be wrong?

Thanks for any help.

A: 

Take care about the autorelease. Functions usually return autorelease objects. You are within an autorelease pool so, it is possible that the image wont exists when you call the last selector. Try assigning the images by using de setter method

[self setProcessedImage:[ImageHelper scaleImage:self.originalImage maxWidth:320 maxHeight:367]];

only if you have declared your variable as an instance property with retain or copy, it will retain the image. Use NSZombieEnabled to see what happens. It seems to be a classic problem of a freed object.

Jorge