チャオ!
動画の向きが取得できなとアレやコレやが出来なくて、アレゲな時があるよね!
ウブだった頃の私は
ダメパターン
+ (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset
{
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize size = [videoTrack naturalSize];
CGAffineTransform txf = [videoTrack preferredTransform];
if (size.width == txf.tx && size.height == txf.ty) {
return UIInterfaceOrientationLandscapeRight;
} else if (txf.tx == 0 && txf.ty == 0) {
return UIInterfaceOrientationLandscapeLeft;
} else if (txf.tx == 0 && txf.ty == size.width) {
return UIInterfaceOrientationPortraitUpsideDown;
} else {
return UIInterfaceOrientationPortrait;
}
}
とかやっていたんだけど、これじゃー取れない動画ががががが・・・
でも下記のやり方でやったら取れないヤツも無事とれたからこっちがオススメ!
オススメパターン
static inline CGFloat RadiansToDegrees(CGFloat radians) {
return radians * 180 / M_PI;
};
+ (UIInterfaceOrientation)orientationForTrackWithAsset:(AVAsset *)asset
{
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGAffineTransform txf = [videoTrack preferredTransform];
CGFloat videoAngleInDegree = RadiansToDegrees(atan2(txf.b, txf.a));
switch ((int)videoAngleInDegree) {
case 0:
return UIInterfaceOrientationLandscapeRight;
case 90:
return UIInterfaceOrientationPortrait;
case 180:
return UIInterfaceOrientationLandscapeLeft;
case -90:
return UIInterfaceOrientationPortraitUpsideDown;
default:
return UIInterfaceOrientationUnknown;
}
}
動画の横幅がおかしいとかあったら試してみて!