【发布时间】:2014-02-20 18:21:51
【问题描述】:
我正在使用 AV Foundation 作为照片应用程序。我已经使用以下代码设置了一个占据屏幕上半部分的预览层:
[_previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];
一旦用户捕获图像,我将其旋转以使其处于正确的纵向(因为 iOS 喜欢将默认图像设置为横向),然后我创建一个新的子图层并将其内容属性设置为捕获的图像。这会使图像显示为屏幕上半部分的新图层。
唯一的问题是图像看起来被拉伸了,经过研究,我了解到您需要裁剪捕获的图像,但在尝试了几个小时后,我无法让我的裁剪正常工作。
希望有人可以推荐我需要的正确裁剪代码。我只想裁剪捕获的图像,使其与用户在拍照时在预览层中看到的完全一致。
这是我的图像捕获代码:
-(IBAction)stillImageCapture {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections){
for (AVCaptureInputPort *port in [connection inputPorts]){
if ([[port mediaType] isEqual:AVMediaTypeVideo]){
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
NSLog(@"about to request a capture from: %@", _stillImageOutput);
[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if(imageDataSampleBuffer) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc]initWithData:imageData];
image = [self rotate:image andOrientation:image.imageOrientation];
CALayer *subLayer = [CALayer layer];
CGImageRef imageRef = image.CGImage;
subLayer.contents = (id)[UIImage imageWithCGImage:imageRef].CGImage;
subLayer.frame = _previewLayer.frame;
[_previewLayer addSublayer:subLayer];
}
}];
}
【问题讨论】:
标签: ios iphone objective-c avfoundation crop