我面对AVAssetTrack的preferredTransform属性的误解。在我的应用程序中,我正在提取视频帧并尝试显示它们,但我不了解帧数据的布局方式以及preferredTransform属性的含义(如果必须将此转换应用于帧数据,那么图像必须可以正确显示,但根本不可见;如果此变换已应用到图像,则过度使用必须将图像恢复为正常状态,但它也不起作用。
在一些教程中,我看到了这段代码来修复转换
extension AVAssetTrack {
var orientation: (orientation: UIImage.Orientation, isPortrait: Bool) {
let transform = self.preferredTransform
var assetOrientation = UIImage.Orientation.up
var isPortrait = false
if transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0 {
assetOrientation = .right
isPortrait = true
} else if transform.a == 0 && transform.b == -1.0 && transform.c == 1.0 && transform.d == 0 {
assetOrientation = .left
isPortrait = true
} else if transform.a == 1.0 && transform.b == 0 && transform.c == 0 && transform.d == 1.0 {
assetOrientation = .up
} else if transform.a == -1.0 && transform.b == 0 && transform.c == 0 && transform.d == -1.0 {
assetOrientation = .down
}
return (assetOrientation, isPortrait)
}
var fixedVideoTransform: CGAffineTransform {
let videoOrientation = self.orientation
var resultTransform = CGAffineTransform.identity
// Now we need fully discard all video transforms and show video as it was in preview
switch videoOrientation.orientation {
case .down:
let rotationFix = CGAffineTransform.identity.translatedBy(x: videoAssetTrack.naturalSize.width - videoAssetTrack.preferredTransform.tx, y: videoAssetTrack.naturalSize.height - videoAssetTrack.preferredTransform.ty)
resultTransform = videoAssetTrack.preferredTransform.concatenating(rotationFix)
case .right:
let rotationFix = CGAffineTransform.identity.translatedBy(x: videoAssetTrack.naturalSize.height - videoAssetTrack.preferredTransform.tx, y: -videoAssetTrack.preferredTransform.ty)
resultTransform = videoAssetTrack.preferredTransform.concatenating(rotationFix)
case .left:
let rotationFix = CGAffineTransform.identity.translatedBy(x: -videoAssetTrack.preferredTransform.tx, y: videoAssetTrack.naturalSize.width - videoAssetTrack.preferredTransform.ty)
resultTransform = videoAssetTrack.preferredTransform.concatenating(rotationFix)
default:
resultTransform = videoAssetTrack.preferredTransform // default landscape orientation nothing need to do there
}
return resultTransform
}
}
这看起来还可以,但是如果视频旋转度不等于90 180,-90或-180度怎么办。
谢谢您的回答。