#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 imageNamed:@"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 *image = [UIImage imageNamed:@"back.png"];
[btnBack setImage:image 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 *)image {
NSLog(@"CAPTURED IMAGE");
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight);
//根据屏幕宽度截图,获得image
image = [self cutMapView:imageView];
//将截图完后的图保存相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image: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 *image = UIGraphicsGetImageFromCurrentImageContext();
//结束截图
UIGraphicsEndImageContext();
return image;
}
#pragma mark 拍完照后走的代理
-(void)didCaptureImageWithData:(NSData *)imageData {
NSLog(@"CAPTURED IMAGE DATA");
//UIImage *image = [[UIImage alloc] initWithData:imageData];
//UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//[self.cameraView removeFromSuperview];
}
#pragma mark 保存相册出错会走的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
//Show error alert if image could not be saved
if (error) [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Image couldn't be saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}