我正在尝试将图像和视频上传到服务器。它工作正常。我面临的问题是从iphone捕获的图片或录制的视频太大,需要花费一些时间才能上传到服务器。如何减少视频数据的大小?

if(asset.mediaType == .video)
{
    var dataMOV : Data?

    manager.requestAVAsset(forVideo: asset, options: option2, resultHandler:  {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) in
        let avURLAsset = asset as? AVURLAsset
        do
        {
            let data = try Data(contentsOf: (avURLAsset?.url)!)
            dataMOV = data
            print("asset data :%@ ", data)
        }
        catch
        {

        }
    })
}

最佳答案

从AssetInfo中,提取资产URL并将其传递给以下方法

func compressVideo(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
        let urlAsset = AVURLAsset(url: inputURL, options: nil)
        guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) else {
            handler(nil)

            return
        }

        exportSession.outputURL = outputURL
        exportSession.outputFileType = AVFileTypeQuickTimeMovie
        exportSession.shouldOptimizeForNetworkUse = true
        exportSession.exportAsynchronously { () -> Void in
            handler(exportSession)
        }
    }

10-08 04:59