UIImagePickerControllerOriginalImage received memory warning and getting crash

1.2k Views Asked by At

I have a UICollectionView and a bar button at top right(CameraViewController1 : UICollectionViewController).The flow is when I take a picture it moves to a new view controller where the image can be cropped.User has two option Use and Cancel after choosing any of this option it gets back the image to the collection view and it gets arranged like cells.I want to take many photos.But I can take up to 3 pictures only where as the app crashes immediately and shows a message "App terminated due to memory pressure".But the worst part is when I tested the same app in iPhone 5 running iOS 7 the crash wasn't happen.When I test the same in iPhone 4 running iOS 7 it gets crashed and produce received memory warning.

Here my code

- (IBAction)TakeaPhoto:(id)sender {

    [[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];

    gallery=0;
    picker1 = [[UIImagePickerController alloc] init];
    picker1.delegate = self;
    self.resizeableCropArea =YES;
    self.cropSize=CGSizeMake(300,350);
    //picker1.allowsEditing = YES;
    picker1.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker1 animated:YES completion:NULL];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:NULL];

    UIImage *image =  [info objectForKey: UIImagePickerControllerOriginalImage];

    image_cap = [self imageTemp:image scaledToSize:CGSizeMake(320, 370)]; 


    dataTemp = UIImageJPEGRepresentation(image,0.0);

    CropViewController *cropController = [[CropViewController alloc] init];

    cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0);

    [original_image addObject:[UIImage imageWithData:Original_img]]; //original_image Nsmutablearray

    NSLog(@"source image=%@",cropController.sourceImage);

    cropController.resizeableCropArea = self.resizeableCropArea;

    cropController.cropSize = self.cropSize;

    cropController.delegate = self;

    Cancel_Image= cropController.sourceImage;

    [self.navigationController pushViewController:cropController animated:YES];

}
1

There are 1 best solutions below

1
Learning Programming On

@Ramanan R R, I m totally agree with the @Rushabh's comment.. You are allocating that UIImagePickerController for many more times, as TakeaPhoto method call you are allocating UIImagePickerConrtoller, it is not necessary to allocate that multiple times. It makes memory spoilage, thats why your app is going to terminate or crash.. Just allocate that one time in viewDidLoad, make sure one more thing that, do UIImagePickerController as a strong property, because in past it took my whole day to solve issue...

Hope this will work for you and your app will run smoothly...:)