#pragma mark 调用相机

-(void)getCamera {

    

    

    

    //Set white status bar

    [self setNeedsStatusBarAppearanceUpdate];

    

    //Instantiate the camera view & assign its frame

    _cameraView = [[CameraSessionView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];

    

    //Set the camera view's delegate and add it as a subview

    _cameraView.delegate = self;

    

    //Apply animation effect to present the camera view

    CATransition *applicationLoadViewIn =[CATransition animation];

    [applicationLoadViewIn setDuration:0.6];

    [applicationLoadViewIn setType:kCATransitionReveal];

    [applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

    [[_cameraView layer]addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

    

    [self.view.window addSubview:_cameraView];

    

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, ViewHeight-kCameraOverlayViewH, ViewWidth, kCameraOverlayViewH)];

    view.backgroundColor = [UIColor whiteColor];

    view.alpha = 0.5;

    [_cameraView addSubview:view];

    

    //隐藏相机视图

    [_cameraView setTopBarColor:[UIColor clearColor]];

    [_cameraView hideFlashButton]; //On iPad flash is not present, hence it wont appear.

    [_cameraView hideCameraToogleButton];

    [_cameraView hideDismissButton];

    

    //添加自定义视图

    [self addCustomViewToCamera];


}


#pragma mark 添加自定义视图到相机上

-(void) addCustomViewToCamera

{

    //scrollview加在自定义视图上

    [_cameraView addSubview:_scrollView];

    _scrollView.frame = CGRectMake(0, ViewHeight - kCameraOverlayViewH, ViewWidth, kScrollViewH);

    

#pragma mark 拍照按钮

    UIButton *btnCapture = [UIButton buttonWithType:UIButtonTypeCustom];

    [btnCapture setImage:[UIImage p_w_picpathNamed:@"takePicture.png"] forState:UIControlStateNormal];

    btnCapture.center = CGPointMake(self.view.center.x, ViewHeight - 71);

    btnCapture.tag = 300;

    btnCapture.bounds = CGRectMake(0, 0, 64, 64);

    [btnCapture addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [_cameraView addSubview:btnCapture];

    

    //下一步

    UIButton *btnNext = [UIButton buttonWithTitle:@"下一步" andTitleColor:kBtnColor addTarget:self action:@selector(clickButton:) andFont:kZTitleFont andBackGroundColor:[UIColor clearColor] andTag:0];

    btnNext.center = CGPointMake(ViewWidth/4.0*3, btnCapture.center.y);

    btnNext.tag = 200;

    btnNext.bounds = CGRectMake(0, 0, 100, 40);

    [_cameraView addSubview:btnNext];

    

    //返回按钮

    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];

    UIImage *p_w_picpath = [UIImage p_w_picpathNamed:@"back.png"];

    [btnBack setImage:p_w_picpath forState:UIControlStateNormal];

    [btnBack addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [btnBack setImageEdgeInsets:UIEdgeInsetsMake(5, 20, 5, 10)];

    btnBack.tag = 100;

    btnBack.frame = CGRectMake(0, 30, 60, 40);

    [_cameraView addSubview:btnBack];

}




#pragma mark  拍完照后走的代理

-(void)didCaptureImage:(UIImage *)p_w_picpath {

    NSLog(@"CAPTURED IMAGE");

    

    UIImageView *p_w_picpathView = [[UIImageView alloc] initWithImage:p_w_picpath];

    p_w_picpathView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);

    

    //根据屏幕宽度截图,获得p_w_picpath

    p_w_picpath = [self cutMapView:p_w_picpathView];

    

    //将截图完后的图保存相册

    UIImageWriteToSavedPhotosAlbum(p_w_picpath, self, @selector(p_w_picpath:didFinishSavingWithError:contextInfo:), nil);

    

    //移除相机

    [self.cameraView removeFromSuperview];

}


#pragma mark - 截取图片

- (UIImage *)cutMapView:(UIView *)theView

{

    //************** 得到图片 *******************

    CGRect rect = CGRectMake(0, 60, ViewWidth, ViewWidth);  //截取图片大小

    

    //开始取图,参数:截图图片大小

    UIGraphicsBeginImageContext(rect.size);

    //截图层放入上下文中

    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];

    //从上下文中获得图片

    UIImage *p_w_picpath = UIGraphicsGetImageFromCurrentImageContext();

    //结束截图

    UIGraphicsEndImageContext();

    

    return p_w_picpath;

}




#pragma mark 拍完照后走的代理

-(void)didCaptureImageWithData:(NSData *)p_w_picpathData {

    NSLog(@"CAPTURED IMAGE DATA");

    //UIImage *p_w_picpath = [[UIImage alloc] initWithData:p_w_picpathData];

    //UIImageWriteToSavedPhotosAlbum(p_w_picpath, self, @selector(p_w_picpath:didFinishSavingWithError:contextInfo:), nil);

    //[self.cameraView removeFromSuperview];

}


#pragma mark 保存相册出错会走的方法

- (void)p_w_picpath:(UIImage *)p_w_picpath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    //Show error alert if p_w_picpath could not be saved

    if (error) [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Image couldn't be saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];

}