【发布时间】:2014-12-11 16:43:56
【问题描述】:
我知道以前有人问过这个问题,我已经在这里尝试了这个问题的所有答案,我认为这有一些基本的东西我不明白,所以到目前为止我的努力是徒劳的。我希望有人能让我觉得自己像个白痴一样挣扎,并为我指明正确的方向。
我要做的就是使用 AVCaptureSession 捕获一个方形 UIImage 并将其显示在一个缩略图视图中,看起来与视频层中的区域完全相同。图像捕获工作正常,但是,当图像添加到我的缩略图图像视图中时,图像的顶部和底部被扩展以显示在 AvCaptureVideoPreviewLayer 中用户不可见的照片区域。
以下代码初始化我的会话,一切正常,但也许我需要在此处更改一些内容以仅捕获用户可见的部分提要:
-(void)setupFeed
{
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if([self.session canAddInput:deviceInput])
{
[self.session addInput:deviceInput];
}
self.cameraPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.cameraPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.cameraPreview.frame;
[self.cameraPreviewLayer setFrame:frame];
[rootLayer insertSublayer:self.cameraPreviewLayer atIndex:0];
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.imageOutput setOutputSettings:outputSettings];
[self.session addOutput:self.imageOutput];
[self.session startRunning];
}
下一段代码是捕捉我的图像,这段代码也可以,裁剪到一边:
[self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if(imageDataSampleBuffer != NULL)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
self.snappedImage = [UIImage imageWithData:imageData];
[self.snappedImage crop:self.snappedImageView.layer.frame];
[self.session stopRunning];;
}
dispatch_async(dispatch_get_main_queue(),
^{
[self presentSnappedImageOptions];
});
}];
}
-(void)presentSnappedImageOptions
{
[self.imagePreview setImage: self.snappedImage];
}
...最后是我用来尝试将图像裁剪为与可见视频层相同的 UIImage 类别方法:
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect
{
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
我们将不胜感激任何帮助。
【问题讨论】:
标签: ios objective-c uiimage avfoundation