iOS:在应用程序中尝试使用相机时,视图控制器崩溃



我有一个按钮,当用户按下它时,他会被导航到相机拍照。

当他拍照时,他按下完成键,然后回到控制器。但当这种情况发生时,导航栏会出现在屏幕上,低于手机的电池/信号栏。奇怪的是这种情况发生在4/4s,而不是3gs

在不了解更多细节的情况下回答这个问题相当困难,但这里有一些代码我用来打开相机,拍照,然后成功关闭相机。该方法可以使用一行代码调用:[self takePhoto];

- (void) takePhoto {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //Clear out the UI first
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; -> use this if you want them to select from the library
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        //Device does not support a camera, show a message if desired
    }
}

然后你需要一个代表来完成这一切,这样程序就知道当拍摄或选择图像时该做什么,以及如何关闭,这就是这个代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.workingImage = nil;
    //I grab the image and store globally, but first I manually scale and rotate it if necessary    
    self.workingImage = [self scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
    //I display the image in my UI view, so this chunk of code will size it properly
    CGRect bound;
    bound.origin = CGPointZero;
    bound.size = img.size;
    imageView.bounds = bound;
    //Set the UI image view and dismiss the controller
    [imageView setImage:self.workingImage];
    [picker dismissModalViewControllerAnimated:YES];
}

显然,确保你的控制器.h正确地实现了委托,就像这样:

@interface ViewController : UIViewController<UIImagePickerControllerDelegate> { ... }

希望这有帮助?

相关内容

  • 没有找到相关文章

最新更新