AVCaptureSession 捕获图像横向模式
Posted
技术标签:
【中文标题】AVCaptureSession 捕获图像横向模式【英文标题】:AVCaptureSession capture image landscape mode 【发布时间】:2013-07-24 06:27:56 【问题描述】:我正在使用 AVCaptureSession 拍照并将图片存储到相册。当我单击按钮时,它会拍摄快照并存储到相册。但是当我使用横向模式时,然后单击它存储横向模式图像的按钮。预览图像时,它处于纵向模式。
代码:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setCaptureSession:[[AVCaptureSession alloc] init]];
[self addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera
[self addStillImageOutput];
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];
// UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
// [newView.layer addSublayer: self.previewLayer];
// [self.view addSubview: newView];
//previewLayer.frame=self.previewLayer.bounds;
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
// [[self previewLayer] setVideoGravity:AVLayerVideoGravityResize];
// self.previewLayer.orientation = AVCaptureVideoOrientationPortrait;
CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer]setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];
[[self captureSession] startRunning];
camera=[UIButton buttonWithType:UIButtonTypeCustom];
[camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
[camera setFrame:CGRectMake(150, 10, 40, 30)];
[camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:camera];
捕捉快照:
- (void)takephoto:(id)sender
[self captureStillImage];
- (void)captureStillImage
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self 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: %@", [self stillImageOutput]);
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
NSLog(@"attachements: %@", exifAttachments);
else
NSLog(@"no attachments");
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self setStillImage:image];
// [image release];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
];
我在横向模式下使用self.previewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
【问题讨论】:
【参考方案1】:除了 previewLayer 方向,您还应该设置 videoOrientation 来纠正这个问题。
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections])
for (AVCaptureInputPort *port in [connection inputPorts])
if ([[port mediaType] isEqual:AVMediaTypeVideo])
videoConnection = connection;
break;
if (videoConnection)
break;
之后
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
通过在横向或纵向模式下检查天气应用程序来设置方向..
【讨论】:
以上是关于AVCaptureSession 捕获图像横向模式的主要内容,如果未能解决你的问题,请参考以下文章
从 AVCaptureSession 捕获的 iPhone 图像比例
在使用 AVCaptureSession 捕获图像之前获取输出图像分辨率
不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?
iOS 相机 AVCaptureSession 在纵向与横向大小或分辨率