gpt4 book ai didi

iPhone AVFoundation 相机方向

转载 作者:行者123 更新时间:2023-12-03 18:14:28 25 4
gpt4 key购买 nike

我一直在绞尽脑汁地试图让 AVFoundation 相机以正确的方向(即设备方向)捕获图片,但我无法让它工作。

我已经查看了教程,观看了 WWDC 演示,并且下载了 WWDC 示例程序,但即使这样也无法实现。

我的应用程序的代码是...

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
[videoConnection setVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if (imageDataSampleBuffer != NULL)
{
//NSLog(@"%d", screenOrientation);

//CMSetAttachment(imageDataSampleBuffer, kCGImagePropertyOrientation, [NSString stringWithFormat:@"%d", screenOrientation], 0);

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];

[self processImage:image];
}
}];

(processImage 使用与 WWDC 代码相同的 writeImage... 方法)

WWDC 应用程序的代码是...

AVCaptureConnection *videoConnection = [AVCamDemoCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
if ([videoConnection isVideoOrientationSupported]) {
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
[delegate captureStillImageFailedWithError:error];
}
}
}];
[library release];
[image release];
} else if (error) {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
[delegate captureStillImageFailedWithError:error];
}
}
}];

在代码的开头,他们将 AVOrientation 设置为纵向,这看起来很奇怪,但我试图让它检测设备的当前方向并使用它。

如您所见,我已放置 [UIApplication sharedApplication]statusBarOrientation 来尝试获取此内容,但它仍然只以纵向保存照片。

任何人都可以对我需要做什么提供任何帮助或建议吗?

谢谢!

奥利弗

最佳答案

好吧,我花了很长时间进行水力压裂,但我已经做到了!

我正在寻找的代码是

[UIDevice currentDevice].orientation;

这样就可以了

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
[videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
}

它工作得很好:D

呜呜呜!

关于iPhone AVFoundation 相机方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3662930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com