在相机视图中保存带有叠加层的图像
Posted
技术标签:
【中文标题】在相机视图中保存带有叠加层的图像【英文标题】:save image with overlay in the camera view 【发布时间】:2014-07-23 10:00:52 【问题描述】:我使用下面的代码在相机视图上添加了一个叠加层,
- (IBAction)takephoto:(id)sender
mediaPicker = [[UIImagePickerController alloc] init];
mediaPicker.delegate=self;
mediaPicker.allowsEditing = YES;
mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// overlay on top of camera lens view
UIImageView *cameraOverlayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Overlay2.png"]];
cameraOverlayView.alpha = 0.0f;
mediaPicker.cameraOverlayView = cameraOverlayView;
// animate the fade in after the shutter opens
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.2f];
cameraOverlayView.alpha = 1.0f;
[UIView commitAnimations];
[self presentViewController:mediaPicker animated:YES completion:Nil];
如何保存带有叠加图像的捕获图像?那就是我需要将捕获的图像和叠加层都保存为单个图像。
【问题讨论】:
我不确定如何使用叠加层保存图像。但是您可以为这个问题做的一件事是“在保存从相机拍摄的图像后,获取该图像并使用 aplha 属性将此图像与覆盖图像与覆盖图像合并。您可以通过覆盖获得捕获的图像” 【参考方案1】: imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = NO;
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.showsCameraControls=NO;
imgPicker.extendedLayoutIncludesOpaqueBars = YES;
imgPicker.edgesForExtendedLayout = YES;
imgPicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
imgPicker.cameraCaptureMode=UIImagePickerControllerQualityTypeHigh;
imgPicker.cameraViewTransform = CGAffineTransformScale(imgPicker.cameraViewTransform, CAMERA_TRANSFORMWidth , CAMERA_TRANSFORM);
UIView* overlayView = [[UIView alloc] initWithFrame:imgPicker.view.frame];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 25.0);
imgPicker.cameraViewTransform = CGAffineTransformScale(translate, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
imgPicker.cameraOverlayView = overlayView;
UIImageView *stripImageVW=[[UIImageView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-70,self.view.frame.size.width,70)];
[stripImageVW setImage:[UIImage imageNamed:@"Black_StripView"]];
[overlayView addSubview:stripImageVW];
【讨论】:
【参考方案2】:我做过类似的事情,你要做的是获取一个UIView
,它将充当一个容器视图,现在向它添加两个图像视图,就像我在下面的代码中所做的那样。创建 Container View 并添加两个 ImageView 使其在您的 storyBoard 中如下所示
现在只需使用此代码
UIGraphicsBeginImageContextWithOptions(
containerView.bounds.size,
view.opaque,
0.0
);
[containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
你会得到的这个img,是两个ImageView的组合图像
【讨论】:
以上是关于在相机视图中保存带有叠加层的图像的主要内容,如果未能解决你的问题,请参考以下文章