简体   繁体   中英

iOS 7 crashes BAD_ACCESS when using UIImagePickerController

I am trying to use a UIImagePickerController to let my users select photos or take photos to be used in my app. But the app seems to crash most of the time whenever a image is selected from their photos or when they choose to use a photo taken. I used NSZombie objects and I get this message:

[NSISRestrictedToNonNegativeVariable retain]: message sent to deallocated instance 0x168c3530

I don't know what is going on here. This same problem occurred when I was presenting a modal view occasionally.

Here is my code for presenting the UIImagePickerController and handling the response:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:CANCEL_TITLE]) {
            return;
        }
        UIImagePickerControllerSourceType sourceType = buttonIndex == 0 ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController* controller = [[UIImagePickerController alloc] init];
        controller.sourceType = sourceType;
        [controller setDelegate:self];
        if (buttonIndex == 1) {
            //PNCoverOverlayView* overlayView = [PNCoverOverlayView viewWithNibNamed:nil];
            //[controller setCameraOverlayView:overlayView];
            [controller setShowsCameraControls:YES];
        }
        [self presentViewController:controller animated:YES completion:nil];
    }

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            UIImage* originalImage = info[UIImagePickerControllerOriginalImage];
            _imageToUpload = originalImage;
            CGFloat scaleAdjust = ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2) ? 0.5 : 1;
            CGFloat length = LENGTH * scaleAdjust;
            if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
                PNCoverOverlayView* overlay = (PNCoverOverlayView*)[picker cameraOverlayView];
                CGSize overlaySize = [overlay frame].size;
                UIImage* cropped = [UIImage imageCroppedToSize:overlaySize fromImage:originalImage];
                _imageToUpload =[UIImage imageResizedToSize:CGSizeMake(length, length) fromImage:cropped];
            } else {
                _imageToUpload = [UIImage imageScaledToMaximumLength:length fromImage:originalImage];
            }
            [self performSegueWithIdentifier:@"reviewPhotoUpload" sender:self];
        }];
    }

I commented out adding a custom overlay to the UIImagePickerController, because that fixed the app crashing when presenting the UIImagePickerController

You should check for the following things:

  1. Are you testing it on device ?

  2. Follow the steps mentioned under Overview in this apple guideline, and take care of the device on which you are running. eg, iPhone or iPad.

  3. Check for isSourceTypeAvailable also.

  4. Also, Check for isCameraDeviceAvailable property.

  5. When trying to get cameraOverlayView , you should check if its not nil . See the documentation of this property for more information.

Hope it helps!

You are instantiating the controller in your function:

UIImagePickerController* controller = [[UIImagePickerController alloc] init];

You should keep a strong reference to the controller in your class, an ivar would be a good choice for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM