我需要在我的应用程序中创建自定义相机叠加层,但我很难让它实际拍照、将相机从正面切换到背面并取消。
我创建了一个 CameraOverlayViewController 类,并在情节提要中设置了所有视图。
当用户选择拍照时,我正在执行以下操作来获取叠加层:
CameraOverlayViewController *customerOverlay = [self.storyboard instantiateViewControllerWithIdentifier:@"camera_overlay"];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.cameraOverlayView = customerOverlay.view;
customerOverlay.pickerRefernce = picker;
[self presentViewController:picker animated:YES completion:nil];
这在显示我希望看到的相机叠加层时效果很好。
这是我的 CameraOverlayViewController 代码:
#import <UIKit/UIKit.h>
@interface CameraOverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) UIImagePickerController *pickerRefernce;
@property (weak, nonatomic) IBOutlet UIButton *cameraButton;
@property (weak, nonatomic) IBOutlet UIView *navBar;
- (IBAction)takePicture:(id)sender;
- (IBAction)switchCamera:(id)sender;
- (IBAction)cancelPicture:(id)sender;
@end
//iphone screen dimensions
#define SCREEN_WIDTH self.view.bounds.size.width
#define SCREEN_HEIGTH self.view.bounds.size.height
@interface CameraOverlayViewController ()
@end
@implementation CameraOverlayViewController
@synthesize pickerRefernce = _pickerRefernce;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
self.navBar.backgroundColor = [UIColor PFYellow];
self.cameraButton.frame = CGRectMake((SCREEN_WIDTH / 2) - (self.cameraButton.frame.size.width / 2), SCREEN_HEIGTH - self.cameraButton.frame.size.height - 20, self.cameraButton.frame.size.width, self.cameraButton.frame.size.height);
}
- (IBAction)takePicture:(id)sender {
[self.pickerRefernce takePicture];
}
- (IBAction)switchCamera:(id)sender {
if(self.pickerRefernce.cameraDevice == UIImagePickerControllerCameraDeviceFront) {
self.pickerRefernce.cameraDevice = UIImagePickerControllerCameraDeviceRear;
} else {
self.pickerRefernce.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
- (IBAction)cancelPicture:(id)sender {
}
基本上,当我按下覆盖视图上的任何按钮(包括取消按钮,它没有导致错误的代码)时,我在 Xcode 中遇到了一个重大错误。
唯一的输出是“(lldb)”并且在 main.m 中抛出一个异常,上面写着“线程 1: EXC_BAD_ACCESS(code = 1, address=0x69786d54)
该应用程序只是继续运行(相机视图仍然显示实时图像),但点击任何东西都没有响应。
此外,我已经点击了继续程序执行按钮至少 200 次,试图让它真正崩溃,但没有任何反应。
我在网上找到的所有示例都已过时(甚至苹果示例“PhotoPicker”使用 xib 文件和我不习惯的奇怪插座),我在这里找到的所有答案都倾向于解释如何显示视图,而不是如何确保它调用所有正确的方法。
任何指导都会很棒,谢谢!