Как получить UIImage в портретной ориентации из 'didOutputSampleBuffer', используя "AVCaptureVideoDataOutputSampleBufferDelegate"? - PullRequest
0 голосов
/ 11 января 2019

UIImage, который я получаю в настоящее время из функции didOutputSampleBuffer с использованием AVCaptureVideoDataOutputSampleBufferDelegate, имеет неправильную ориентацию (повернут на 90 градусов).

Мне нужно отправить его на модель. Итак, я попытался повернуть его обратно, используя функцию, найденную в stackoverflow.

Я использовал эту функцию:

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees image:(UIImage*)image
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;

// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();

// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

//   // Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), [image CGImage]);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

sample image

Но изображение как-то искажается, и повернутые изображения выглядят голубоватыми оттенками. Модель не может обнаружить какие-либо особенности изображения. Пожалуйста, ведите меня в правильном направлении.

...