如何使用“ AVCaptureVideoDataOutputSampleBufferDelegate”从“ didOutputSampleBuffer”获取方向肖像中的UIImage?

时间:2019-01-11 11:04:50

标签: ios objective-c uiimage cmsamplebuffer

我当前使用“ AVCaptureVideoDataOutputSampleBufferDelegate”从“ didOutputSampleBuffer”函数获取的UIImage方向错误(旋转了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

但是图像以某种方式被损坏,旋转后的图像看起来像是蓝色。模型无法从图像中检测到任何特征。请指引我正确的方向。

0 个答案:

没有答案