0
녹음 된 비디오를 mp4로 변환 한 다음 내 서버에 업로드하려고합니다. 저는 값을 캐스팅 할 때 문제가 발생하여 어디에서나 솔루션을 검색했습니다. 특정 지점에서 응용 프로그램이 중단되고 'NSData'오류에 '__NSCFString'유형의 값을 캐스팅 할 수 없습니다. 어떤 도움을 주셔서 감사합니다.형식 '__NSCFString'의 값을 'NSData'로 변환 할 수 없습니다.
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession?.outputURL as Any)
let mediaPath = exportSession?.outputURL?.path as NSString!
self.uploadVideo(mediaPath as! Data)
// 업로드 비디오
func uploadVideo(_ mediaPath: Data) {
func createBodyWithParams(_ parameters: [String: String]?, filePathKey: String?, mediaPath: Data, boundary: String) -> Data {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
var filename = ""
if imageSelected == true {
filename = "video-\(uuid).mp4"
}
let mimetype = "video/mp4"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(String(describing: "Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)))
body.append(mediaPath)
body.append(String(format: "\r\n").data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
return body as Data
}
let id = user!["id"] as! String
uuid = UUID().uuidString
let url = URL(string: "http://www.foo.com/videoposts.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let param = [
"id" : id,
"uuid" : uuid
]
print("just passed videopost page")
// body
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
// if picture is selected, compress it by half
//let imageData = Data()
// ... body
request.httpBody = createBodyWithParams(param, filePathKey: "filename", mediaPath: AnyObject, boundary: boundary)
// launch session
URLSession.shared.dataTask(with: request) { data, response, error in
// get main queu to communicate back to user
DispatchQueue.main.async(execute: {
if error == nil {
do {
// json containes $returnArray from php
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
// declare new var to store json inf
guard let parseJSON = json else {
print("Error while parsing")
return
}
// get message from $returnArray["message"]
let message = parseJSON["message"]
// if there is some message - post is made
if message != nil {
// reset UI
self.postBtn.alpha = 0.4
self.imageSelected = false
// switch to another scene
self.tabBarController?.selectedIndex = 4
}
} catch {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} else {
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
})
}.resume()
}
감사합니다. @vadian, 불행히도 여전히 나에게 같은 오류가 발생합니다. 다른 제안. – techgirl
이 줄에 오류가 계속 발생합니다. request.httpBody = createBodyWithParams (param, filePathKey : "filename", mediaPath : Data, boundary : boundary) – techgirl