问题描述
我正在使用AVAssetExportSession
在iOS应用中导出视频。为了以正确的方向呈现视频,我使用了AVAssetTrack
的{{1}}。对于某些源视频,此属性似乎具有错误的值,并且视频在结果中显示为偏移或完全黑色。我该如何解决?
解决方法
preferredTransform
是preferredTransform
。属性CGAffineTransform
,a
,b
,c
是反射矩阵和旋转矩阵的串联,而属性d
和tx
描述了翻译。在我用错误的ty
观察到的所有情况下,反射/旋转部分似乎都是正确的,只有平移部分包含错误的值。一个可靠的解决方法似乎是检查preferredTransform
,a
,b
,c
(总共8个案例,每个案例对应于d
中的案例),并且相应地更新UIImageOrientation
和tx
:
ty
,
我认为我最终做了一些更强大的事情,我根据它最终的位置取消了转换:
auto naturalFrame = CGRectMake(0,naturalSize.width,naturalSize.height);
auto preferredFrame = CGRectApplyAffineTransform(naturalFrame,preferredTransform);
preferredTransform.tx -= preferredFrame.origin.x;
preferredTransform.ty -= preferredFrame.origin.y;
请注意,您不能只在 (0,0)
上应用变换,因为 CGRect.origin
会考虑翻转之类的事情。