Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePicker crashes on ios7/iphone 4s

I have an app which uses the UIImagePickerController to allow the user to take photos.

I have reduced the test case to the simplest sequence in a single view controller app. Here is my code.

//
//  CTViewController.h
//  Camera Test
//

#import <UIKit/UIKit.h>

@interface CTViewController : UIViewController <UINavigationControllerDelegate,   UIImagePickerControllerDelegate>

@property (nonatomic, retain) UIImagePickerController *cameraController;

- (IBAction)takePicture:(id)sender;

@end

the body of the code is as follows:

//
//  CTViewController.m
//  Camera Test

#import "CTViewController.h"

....

- (void)didReceiveMemoryWarning
{
    NSLog(@"%s", __func__);

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePicture:(id)sender
{
    self.cameraController = [[UIImagePickerController alloc] init];

    self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.cameraController.allowsEditing = YES;
    self.cameraController.delegate = self;
    [self presentViewController:self.cameraController animated:YES completion:nil];
}

'takePicture' is hooked up to a button I can press in the middle of the screen.

On ios 6 everything works perfectly, but on ios 7 I get a cascade of memory warnings once the viewcontroller is presented. Thus:

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Received memory warning.
-[CTViewController didReceiveMemoryWarning]

.... <here I take a picture, which I do nothing with>

Received memory warning.
-[CTViewController didReceiveMemoryWarning]
Received memory warning.
-[CTViewController didReceiveMemoryWarning]

.... <I get a cascade of these warnings>

The app is built using the ios 7.0 sdk using xcode 5. The same problem occurs if I build with the ios 6.1 sdk, but run on ios7. Building with the ios 6.1 sdk and running on ios 6.1.3 causes no messages and no problems.

My complete app crashes 50% of the time on ios 7. I respond to the memory warning by throwing a lot of stuff (images mostly) out of memory and profiling confirms this, but I still get the cascade of warnings (i.e. they continue after the memory is freed).

If I use the front camera, choose from the gallery or use an iPad 3, there are no messages. I therefore suspect that the memory problem is associated with the size of the UIImagePickerController when the rear camera is used.

I have fully explored stackoverflow and have looked particularly at this post - UIImagePickerController error: Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7

I have tried everything suggested, but my simple test app precludes most of the explanations.

Any thoughts? Should I abandon support for the iPhone 4S? I have no yet confirmed the problem on the iPhone 5, but I will update this question as soon as I have.

:-)

like image 447
Martin Dunsmuir Avatar asked Oct 16 '13 06:10

Martin Dunsmuir


1 Answers

I would recommend you not using a property for the image picker, have a local object instead. See below my code that works fine on IOS7 as well.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = YES;

    [self presentViewController:imagePicker animated:YES completion:nil];
}
like image 128
Calin Chitu Avatar answered Oct 20 '22 00:10

Calin Chitu