【发布时间】:2015-05-03 13:57:17
【问题描述】:
在抓取可视屏幕的裁剪 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吗?
标签: ios objective-c avcapturesession