我正在使用 AVFoundation 相机中的缩放功能,我通过缩放具有 AVCaptureVideoPreviewLayer 的视图来实现缩放。
现在我想捕捉缩放图像。
这是我添加 AVFoundation Video PreviewLayer 进行查看的代码:
// create a uiview subclass for showing the camera feed
UIView *previewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
[[self view] addSubview:previewView];
CGRect layerRect = CGRectMake(0, 0, 320, 430);
[[self avCaptureVideoPreviewLayer] setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect))];
// add the video previewview layer to the preview view
[[previewView layer] addSublayer:[self avCaptureVideoPreviewLayer]];
缩放预览视图的代码
// zoom the preview view using core graphics
[previewView setTransform:CGAffineTransformMakeScale(2.0, 2.0 )];
现在我想从 PreviewView 捕获这个缩放图像
提前致谢。
最后,我设法在我的应用程序中添加捕获缩放照片。它有点难看,但它有效。我使用来自网络的 UIImage+resize 代码,然后将图像缩放到缩放比例,并计算缩放区域的偏移位置[在相机视图中可见],然后裁剪它。
现在,当缩放比例达到最大(即 6 倍)时,我的应用程序有时会崩溃。由于缩放图像太大,我收到内存问题,我会将此作为另一个问题。
希望这段代码对某人有用。
if (cameraZoom > 1.0)
{
// save the images original size
CGSize orgSize = [image size];
// resize the image to the zoom scale
image = [image resizedImage:CGSizeMake(image.size.width * cameraZoom, image.size.height *cameraZoom) interpolationQuality:kCGInterpolationNone];
// now calculate the offset x and offset y
CGFloat offsetX = ( image.size.width / 2 ) - (orgSize.width /2) ;
CGFloat offsetY = ( image.size.height / 2 ) - (orgSize.height /2);
// crop the image from the offset position to the original width and height
image = [image croppedImage:CGRectMake(offsetX, offsetY, orgSize.width, orgSize.height)];
}
UIButton *imgBtn = (UIButton *)[self.view viewWithTag:500];
[imgBtn setImage:image forState:UIControlStateNormal];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)