我需要在我的应用程序中创建自定义相机覆盖,我很难让它实际拍照,从前到后切换相机,然后取消。
我创建了一个CameraOverlayViewController类,并在storyboard中设置了所有视图。
当用户选择拍照时,我正在做的是获取叠加效果:
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中抛出异常,其中显示“Thread 1:EXC_BAD_ACCESS(code = 1,address = 0x69786d54)
应用程序一直在运行(摄像机视图仍然显示实时图像)但是点击任何内容都没有响应。
另外,我已经点击了继续程序执行按钮至少200次试图让它实际崩溃,但没有任何反应。
我在网上找到的所有例子都已过时(即使苹果示例“PhotoPicker”使用的是xib文件和我不熟悉的奇怪插座),我在这里找到的所有答案都倾向于解释如何让视图显示起来,而不是如何确保它调用所有正确的方法。
任何指导都会很棒,谢谢!