gpt4 book ai didi

iphone - AVCaptureVideoOrientation 横向模式会导致静态图像颠倒

转载 作者:行者123 更新时间:2023-12-03 19:36:22 24 4
gpt4 key购买 nike

我使用 AVCaptureSession 来拍照并将图片存储到相册中。当我单击按钮时,它会拍摄快照并将其存储到相册中。但是当我使用横向模式时,然后单击它存储横向模式的按钮会导致颠倒的静态图像。

enter image description here

代码:

- (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]] ];

[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

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];

}];


}

最佳答案

您需要根据设备的方向设置 videoConnection 的 videoOrientation 属性。设置 AVCaptureConnection 后,在 captureStillImage 中执行此操作。

    UIDeviceOrientation deviceOrientation = 
[[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation avcaptureOrientation;
if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )
avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight;

else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )
avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft;

[videoConnection setVideoOrientation:avcaptureOrientation];

关于iphone - AVCaptureVideoOrientation 横向模式会导致静态图像颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849334/

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