Ami*_*irW 10 iphone avfoundation
我正在写一个iPhone应用程序,它使用AVFoundation拍照并剪裁.该应用程序类似于QR代码阅读器:它使用带覆盖的AVCaptureVideoPreviewLayer.叠加层有一个正方形.我想裁剪图像,以便裁剪的图像正好是用户在广场内的位置.
预览图层具有重力AVLayerVideoGravityResizeAspectFill.
看起来相机实际拍摄的内容并不完全是用户在预览图层中看到的内容.这意味着我需要从预览坐标系移动到捕获的图像坐标系,以便我可以裁剪图像.为此,我认为我需要以下参数:1.视图大小和捕获图像大小之间的比例.2.告知捕获图像的哪个部分与预览图层中显示的内容匹配的信息.
有没有人知道如何获取此信息,或者是否有不同的方法来裁剪图像.
(ps捕获预览的屏幕截图不是一个选项,因为我知道它可能会导致应用程序被拒绝).
先感谢您
希望这符合您的要求
- (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
NSLog(@"---------");
NSLog(@"*cropRect.origin.y=%f",cropRect.origin.y);
NSLog(@"*cropRect.origin.x=%f",cropRect.origin.x);
NSLog(@"*cropRect.size.width=%f",cropRect.size.width);
NSLog(@"*cropRect.size.height=%f",cropRect.size.height);
NSLog(@"---------");
NSLog(@"*size.width=%f",size.width);
NSLog(@"*size.height=%f",size.height);
CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -size.height);
CGContextDrawImage(context, myRect, subImage);
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(subImage);
return croppedImage;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4051 次 |
| 最近记录: |