Я рвал волосы, пытаясь заставить камеру 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 использует тот же метод writeImage... как код WWDC)
и код из приложения 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, чтобы попытаться получить это, но он сохраняет только фотографии в портрете.
Может ли кто-нибудь предложить какую-либо помощь или совет относительно того, что мне нужно делать?
Спасибо!
Оливер