当我尝试截取可见屏幕的裁剪 UIImage 时,我的 AVCaptureVideoPreviewLayer 方法出了问题。目前它能工作,但不能输出我所需的正确裁剪。
我正在尝试输出一个正方形,但(看起来)似乎给出了完整高度并压缩图像。
之前的图像显示了实时屏幕,而之后的图像显示了按下捕获按钮后的图像。您可以看到它已垂直变化以适应正方形,但高度未被垂直裁剪。
捕获图像代码
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
if (imageSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
[self processImage:[UIImage imageWithData:imageData]];
}
}];
剪裁代码
- (void) processImage:(UIImage *)image { //process captured image, crop, resize and rotate
haveImage = YES;
CGRect deviceScreen = _previewLayer.bounds;
CGFloat width = deviceScreen.size.width;
CGFloat height = deviceScreen.size.height;
NSLog(@"WIDTH %f", width); // Outputing 320
NSLog(@"HEIGHT %f", height); // Outputting 320
UIGraphicsBeginImageContext(CGSizeMake(width, width));
[image drawInRect: CGRectMake(0, 0, width, width)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect cropRect = CGRectMake(0, 0, width, width);
CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
CGImageRelease(imageRef);
[captureImageGrab setImage:[UIImage imageWithCGImage:imageRef]];
}
processImage
中记录图像大小?还有captureImageGrab
是什么?它是一个UIImageView
吗? - Yuchen