Alpinista
Alpinista

Reputation: 3741

video orientation UIImagePickerController

I am choosing videos using UIImagePickerController, then uploading the video to a web server using ASIHTTPRequest. However, videos that were shot with the iPhone held upside-down in landscape or portrait are inverted on the web server. When those uploaded videos are viewed on an iPhone, they are also scaled thy 75% or so vertically so that they appear squished.

Is there a way to determine the video orientation (including whether it was shot upside-down) of a video chosen using UIImagePickerController?

Also, is there a way to change the orientation of the video before uploading?

I'd also like to not allow uploads of video shot in portrait orientation.

Thanks

Upvotes: 1

Views: 4388

Answers (2)

Yunus Tek
Yunus Tek

Reputation: 626

You can learn video portrait and assetOrientation with the func:

 static func orientationFromTransform(_ transform: CGAffineTransform) -> (orientation: UIImageOrientation, isPortrait: Bool) {
    var assetOrientation = UIImageOrientation.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)
}

Sources: https://www.raywenderlich.com/5135-how-to-play-record-and-merge-videos-in-ios-and-swift

Upvotes: 0

Alpinista
Alpinista

Reputation: 3741

Found the answer in anther post. AVAsset gives you two properties, [avAsset naturalSize] and [avAsset preferredTransform], that allow you to determine the video orientation.

Here's the related post:

How to detect (iPhone SDK) if a video file was recorded in portrait orientation, or landscape.

Upvotes: 1

Related Questions