使用UIImagePickerController时,iOS 7会崩溃BAD_ACCESS

时间:2014-02-05 20:05:05

标签: ios iphone objective-c ios7

我正在尝试使用UIImagePickerController让我的用户选择照片或拍照以在我的应用中使用。但是,无论何时从照片中选择图像或选择使用照片,应用程序似乎都会崩溃。我使用了NSZombie对象,我收到了这条消息:

  

[NSISRestrictedToNonNegativeVariable retain]:发送到解除分配的实例0x168c3530的消息

我不知道这里发生了什么。当我偶尔提出一个模态视图时,也出现了同样的问题。

这是我展示UIImagePickerController并处理响应的代码:

- (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];
        }];
    }

我注释掉了向UIImagePickerController添加自定义叠加层,因为它修复了在呈现UIImagePickerController时应用崩溃的问题

2 个答案:

答案 0 :(得分:0)

您应该检查以下内容:

  
      
  1. 您是否在设备上进行测试?

  2.   
  3. 按照this apple指南中Overview下提到的步骤操作,并处理您正在运行的设备。例如。,   iPhone或iPad。

  4.   
  5. 同时检查isSourceTypeAvailable

  6.   
  7. 此外,检查isCameraDeviceAvailable属性。

  8.   
  9. 尝试获取cameraOverlayView时,您应该检查它是否不是nil。有关更多信息,请参阅此属性的文档   信息。

  10.   

希望它有所帮助!

答案 1 :(得分:0)

您正在函数中实例化控制器:

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

你应该在课堂上对控制器进行强有力的参考,ivar将是一个不错的选择。