gpt4 book ai didi

ios - 无法快速使用 GCDWebServer 在网络浏览器上获取视频

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:30 25 4
gpt4 key购买 nike

我创建了一个小型演示应用程序,其中应用程序使用 UIImagePickerController 从 iPhone 访问图像和视频。当我选择任何图像或视频时,应用程序会在文档目录中创建它的副本(图像或视频)。并使用 GCDWebserver 在 iPhone 上创建一个 Web 服务器,需要公开所选的图像或视频。但它不起作用。

这是示例代码,不知道我哪里错了。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var btnselect: UIButton!
let videoPicker = UIImagePickerController()

@IBAction func btnSelect(_ sender: Any) {

if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
myPickerController.allowsEditing = false
self.present(myPickerController, animated: true, completion: nil)
}

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

// 1. saving video to documents directory

let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL
let videoData = NSData(contentsOf: videoURL as URL)
let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)

let newPath = path.appendingPathComponent("/myTestVideo.mp4")
do {
try videoData?.write(to: newPath)
} catch {
print(error)
}


// 2. Create web server on iPhone using GCDWebServer

let webServer = GCDWebServer()
webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
return GCDWebServerDataResponse(newPath)

})
webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

}
}

我需要此 GCD Web 服务器托管/访问所选的(图像或视频)。

最佳答案

您可以使用 GCDWebServerDataResponse 将文件作为响应传递,如下所示:

webServer!.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
let path = Bundle.main.path(forResource: "sample", ofType: "mp4")
let handler = FileHandle.init(forReadingAtPath: path!)
return GCDWebServerDataResponse(data: (handler?.readDataToEndOfFile())!, contentType: "video/mp4")
})

请注意,当您需要提供文件的内容类型和数据时。

关于ios - 无法快速使用 GCDWebServer 在网络浏览器上获取视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56240669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com