Ios avassetexport在景观中返回视频

Ios avassetexport在景观中返回视频,ios,swift,Ios,Swift,我已经创建了这个函数,它可以获取一个以肖像模式捕获的视频。然而,当我保存avassetexport并显示它时,它似乎将其识别为风景,我如何确保将其创建为肖像视频 func createVideo() -> AVAssetExportSession { let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString

我已经创建了这个函数,它可以获取一个以肖像模式捕获的视频。然而,当我保存avassetexport并显示它时,它似乎将其识别为风景,我如何确保将其创建为肖像视频

func createVideo() -> AVAssetExportSession {

    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString

    let fileURL = NSURL(fileURLWithPath: "\(documentsPath)/pre.mov")

    let composition = AVMutableComposition()
    let vidAsset = AVURLAsset(URL: fileURL, options: nil)

    // get video track
    let vtrack =  vidAsset.tracksWithMediaType(AVMediaTypeVideo)
    let videoTrack:AVAssetTrack = vtrack[0]
    let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)


    do {
        let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
        try compositionvideoTrack.insertTimeRange(vid_timerange, ofTrack: videoTrack, atTime: kCMTimeZero)

        compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
    } catch {
        print(error)
    }

    //Get the video
    let fullSizeImage = videoTrack
    print(fullSizeImage.naturalSize)

    let newOverLayHeight = fullSizeImage.naturalSize.width / self.containerView!.frame.width * self.containerView!.frame.height

    UIGraphicsBeginImageContext(CGSizeMake(fullSizeImage.naturalSize.width, newOverLayHeight));
    self.containerView!.drawViewHierarchyInRect(CGRectMake(0, 0, fullSizeImage.naturalSize.width, newOverLayHeight), afterScreenUpdates: true)
    let overlayImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    let imglogo = UIImage(named: "image.png")
    let imglayer = CALayer()
    imglayer.contents = imglogo?.CGImage
    imglayer.frame = CGRectMake(0,fullSizeImage.naturalSize.height - newOverLayHeight, overlayImage.size.width, overlayImage.size.height)

    let videolayer = CALayer()
    videolayer.frame = CGRectMake(0, 0, fullSizeImage.naturalSize.width, fullSizeImage.naturalSize.height)

    let parentlayer = CALayer()
    parentlayer.frame = CGRectMake(0, 0, fullSizeImage.naturalSize.width, fullSizeImage.naturalSize.height)
    parentlayer.addSublayer(videolayer)
    parentlayer.addSublayer(imglayer)


    let layercomposition = AVMutableVideoComposition()
    layercomposition.frameDuration = CMTimeMake(1, 30)
    layercomposition.renderSize = fullSizeImage.naturalSize
    layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, inLayer: parentlayer)

    let instruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
    let videotrack = composition.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
    let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)

    instruction.layerInstructions = NSArray(object: layerinstruction) as! [AVVideoCompositionLayerInstruction]
    layercomposition.instructions = NSArray(object: instruction) as! [AVVideoCompositionInstructionProtocol]


    //  create new file to receive data
    let docsDir: AnyObject = documentsPath
    let movieFilePath = docsDir.stringByAppendingPathComponent("result.mov")
    let movieDestinationUrl = NSURL(fileURLWithPath: movieFilePath)
    _ = try? NSFileManager().removeItemAtURL(movieDestinationUrl)

    let preFilePath = docsDir.stringByAppendingPathComponent("pre.mov")
    let preDestinationUrl = NSURL(fileURLWithPath: preFilePath)
    _ = try? NSFileManager().removeItemAtURL(preDestinationUrl)

    // use AVAssetExportSession to export video
    let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
    assetExport!.outputFileType = AVFileTypeQuickTimeMovie
    assetExport!.outputURL = movieDestinationUrl
    assetExport!.videoComposition = layercomposition



    self.movieUrl = movieFilePath

    return assetExport!

}