我正在尝试在我的应用程序中为相机创建一个叠加层。我已经成功地在屏幕上显示了一个按钮,但我不知道为按钮的选择器设置什么。所以我有一个名为 CameraOverlay 的类,它所做的只是在屏幕上放一个按钮。然后我有另一个课程,我创建相机并在相机完成拍照后处理所有事情。我该怎么做才能在 CameraOverlay 类中按下按钮时设备拍摄照片?这是 CameraOverlay 类的代码:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//Create Button
[self createCameraButton];
self.backgroundColor = [UIColor clearColor];
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
}
return self;
}
-(void)createCameraButton{
self.cameraButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.cameraButton addTarget:self
action:@selector(takePicture)
forControlEvents:UIControlEventTouchUpInside];
[self.cameraButton setBackgroundImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
self.cameraButton.frame = CGRectMake(140, 500, 50, 50);
[self addSubview:self.cameraButton];
}
这是我创建 UIImagePickerController 的方法:
//create an overlay view instance
CameraOverlayView *overlay = [[CameraOverlayView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
self.imagePicker.navigationBarHidden = YES;
self.imagePicker.toolbarHidden = YES;
self.imagePicker.wantsFullScreenLayout = YES;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.videoMaximumDuration = 10;
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
self.imagePicker.showsCameraControls = NO;
self.imagePicker.cameraOverlayView = overlay;
[self presentViewController:self.imagePicker animated:NO completion:nil];