My iOS app using Swift 4 has the ability to save or share videos that were taken from the front or back camera. In the instance where I take a selfie video, using the front facing camera, and share the video to another application, it sends it upside down even though it saves with the correct orientation. All the necessary transforms take place before saving or sending as far as I'm aware.
Update - I found that the video is shown flipped sharing to Facebook or Messenger, but not Mail. I can still save the video and then upload it to Facebook or Messenger, and it will be in the correct orientation.
I've narrowed it down to being a difference between saving and sharing, but I don't know what it is.
Saving - Sends correct orientation
For saving, a URL is sent to my save function. assetCollection is a PHAssetCollection. A PHAssetCollectionChangeRequest takes place and the video is saved.
func save(URL: URL, completion : @escaping () -> ()) {
if assetCollection == nil {
print("error upstream. save skipped")
return // if there was an error upstream, skip the save
}
PHPhotoLibrary.shared().performChanges({
print("saving")
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL)
let assetPlaceHolder = assetChangeRequest?.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
if self.assetCollection.estimatedAssetCount == 0
{
albumChangeRequest!.addAssets(enumeration)
}
else {
albumChangeRequest!.insertAssets(enumeration, at: [0])
}
}, completionHandler: { status , error in
completion( )
})
}
Sharing - Sends incorrect orientation
When sharing to other apps, NSData from the video URL (self.fileURL) is written to a file path, where a new URL is created and put into the objectsToShare array.
let urlData = NSData(contentsOf: self.fileURL)
if urlData != nil {
let paths:[String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let docDirectory:String = paths[0]
let filePath:String = "\(docDirectory)/tmpVideo.mp4"
urlData?.write(toFile: filePath, atomically: true)
let videoLink = NSURL(fileURLWithPath: filePath)
let objectsToShare:[NSURL] = [videoLink]
let activity: UIActivityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
DispatchQueue.main.async {
UIApplication.topViewController?.present(activity, animated: true, completion: nil)
}
}
Does PHPhotoLibrary preserve orientation transforms made to video, while sharing doesn't? How can I make it behave the same when sharing?
Just as a side note, I also tried saving the video to my photo library, and then choosing the video from my library in another app and sending, and the orientation is correct. Only sends upside down with my share code.
How the video is stored temporarily to be saved or shared later:
I first use a temp directory. This is to preview the video before saving or sharing. Here "url" is where I export my video, which is used by the save or share functions later.
let tempDir = NSTemporaryDirectory()
let url = NSURL(fileURLWithPath: tempDir).appendingPathComponent("tmpMov.mov")