问题
My UIImagePickerController freezes and the camera closes when the button to flip the camera from front to back is pressed. This is how I initialize the image picker controller object within the project (the rest of the code was ommitted) from the methods as it is irrelevant to the UIimagepickercontroller object.
//In my .h file
UIImagePickerController * imgPicker;
//in my .m file
-(void)viewDidLoad {
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.allowsEditing = YES;
}
-(void) takePicture {
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imgPicker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
masterImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
if(masterImage.image == nil) {
masterImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
}
[self dismissModalViewControllerAnimated:YES];
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissModalViewControllerAnimated:YES];
}
-(void) releaseOutlets {
[imgPicker release];
}
回答1:
Just for rule, change your code. Instead of:
UIImagePickerController * imgPicker;
Write in your .h file:
@property (nonatomic, strong) UIImagePickerController * imgPicker;
than synthesize it in your .m file:
imgPicker = _imgPicker;
and next every call to this property call with self.
回答2:
- first of all, you should not alloc init in viewdidload method. Do all ur allocations in -init method.
- add property as suggested by edzio27.
- test again
if problem persists: - check if u are "receiving memory warning". In case of memory warning ur viewdidload method is called again. In case u keep ur alloc init in that method u will be creating new instance everytime.
We faced a similar issue with MPMoviePlayerController. not sure if u have the same issue.
来源:https://stackoverflow.com/questions/13212168/uiimagepickercontroller-freezes-when-camera-flips