iphone - 来自 AVCaptureStillImageOutput 的 UIImage 方向不正确
<p><p>我正在尝试从 AVCaptureStillImageOutput 捕获像素数据,并注意到在将图像裁剪为 CGImage 后,它会重新定向。为了测试这一点,我将一个临时图像输出到照片库。现在我注意到,即使在图像被裁剪之前,它的缩略图也会旋转,而完整图像则不会。 (当我将 UIImage 传递给需要适当尺寸的图像的自定义 pixelDataManager 时,这后来成为一个问题。)</p>
<p>设置 <code>captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;</code> 和 <code>;</code> 似乎没有任何改变。</p>
<p>有什么想法吗???
我会把它分成几个部分...</p>
<p><strong>1) 设置 session 、输入和输出:</strong></p>
<pre><code> // Create a capture session
AVCaptureSession *session = [ init];
session.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [ initWithSession:session];
// Add capture layer to visible view
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResize;
captureVideoPreviewLayer.frame = screenBounds;
captureVideoPreviewLayer.bounds = screenBounds;
captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationPortrait;
;
AVCaptureDevice *device = ;
// Setup error handling
NSError *error = nil;
AVCaptureDeviceInput *input = ;
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
;
// Start session
;
// Output image data
stillImageOutput = [ init];
NSDictionary *outputSettings = [ initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
;
;
</code></pre>
<p><strong>2) 设置视频连接:</strong></p>
<pre><code>AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in )
{
if ([ isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
if ()
{
;
}
</code></pre>
<p><strong>3) 输出捕获的图像:</strong></p>
<pre><code>NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
NSData *imageData = ;
UIImage *image = [ initWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//......
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当您执行此操作时,虽然图像是作为 UIImage 提供给您的,但它实际上使用的是具有不同原点(我认为是左下角)的底层 Quartz CGImage 数据,这意味着当您使用图像时,它会旋转到一边。</p>
<p>我找到了一个 C 函数,您可以使用 UIImage 作为参数调用它来修复它并返回固定的 UIImage。</p>
<p> <a href="http://blog.logichigh.com/2008/06/05/uiimage-fix/" rel="noreferrer noopener nofollow">http://blog.logichigh.com/2008/06/05/uiimage-fix/</a> </p></p>
<p style="font-size: 20px;">关于iphone - 来自 AVCaptureStillImageOutput 的 UIImage 方向不正确,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/7005211/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/7005211/
</a>
</p>
页:
[1]