写真をカメラロールに保存する際に間違った方向を引き起こすavcapturemoviefileoutput
-
28-10-2019 - |
質問
CapturestillimageasynchronelyfromConnectionに奇妙な問題があります。ビデオがミラーリングされている間にjpegstillimagensdatarepresentationを使用して画像を保存すると、カメラロールの画像は時計回りに90度回転します。ただし、ミラーリングされていない場合は、オリエンテーションは問題ありません。コードを投稿しますが、他の誰かがこの問題を抱えている/修正を知っていますか?
更新:いくつかのテストを実行したばかりで、高さと幅(640x480)は問題なく、デバイスの向きを反映しています。肖像画で写真を撮るとき、それはuiimageorientationleftを報告し、ミラーリングすると、uiimageorientationleftmirroredを報告します。
更新2:カメラロールで保存された写真を表示すると、画像のプレビューには正しい方向があります。写真の間をスワイプすると画像がそうであるように、写真が完全にロードされたときに90度回転します。これはカメラロールの問題でしょうか? (私は4.3.3です)
- (void) captureImageAndSaveToCameraRoll
{
AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
if ([stillImageConnection isVideoOrientationSupported])
[stillImageConnection setVideoOrientation:[self orientation]];
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
if (error) {
if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
[[self delegate] captureManager:self didFailWithError:error];
}
}
};
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:completionBlock];
[image release];
[library release];
}
else
completionBlock(nil, error);
if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) {
[[self delegate] captureManagerStillImageCaptured:self];
}
}];
}
解決
新しく作成された画像には、ここで想定しているように、オリエンテーションセットが本当にあるかどうか疑問に思っています。
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation: (ALAssetOrientation) [image imageOrientation] completionBlock:completionBlock];
[image imageOrientation]
特に、特にキャストが必要な場合、問題の原因の潜在的なようです...
あなたはどこかで画像の向きを見ていると言います、それは [image imageOrientation]
で作成した直後:
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation: imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
あなたがこのメタデータが中にあると仮定しているのだろうかと思います imageData
から返されます AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:
実際にそれが画像ピクセルデータ自体のみを含む場合は?
他のヒント
によると いくつかのレポート, 、2009年のiPhoneの写真アプリケーションは、ミラーリングされたオリエンテーションをまったくサポートしていませんでした。確かに彼らはこれを部分的に修正しますが、場合によってはまだバグがあります(たとえば、uiimageViewが処理しないことは知っています contentStretch
場合によっては正しく)。これはテストするのに十分簡単なはずです。そのブログのサンプル画像をカメラロールにロードして、何が起こるかを確認してください。
で説明されているuiimageのカテゴリを作成します iOS UIIMAGEPICKERCONTROLLERアップロード後の画像向けの方向
ライブラリに保存する前に、画像にこの方法を呼び出してください。