如何使相机叠加拍照

时间:2014-07-08 19:07:34

标签: ios camera uiimagepickercontroller camera-overlay cameraoverlayview

我正在尝试在我的应用中为相机创建叠加层。我已成功向屏幕显示按钮,但我不知道为按钮的选择器设置了什么。所以我有一个名为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];

1 个答案:

答案 0 :(得分:0)

    -(void) takePicture:(id)sender{
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        picker.showsCameraControls = NO;

        [self presentViewController:picker animated:YES
                         completion:^ {
                             [picker takePicture];
                         }];}
    }