레퍼런스도 없고 인터넷 참고용 코딩도 지저분하지만 일단 복붙
굉장히 더럽고 참고용이기때문에 필요하면 참고한다.
func generateBoundaryString() -> String
{
return "Boundary-\(NSUUID().UUIDString)"
}
func imageUploadRaw(image:UIImage){
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.projectoxford.ai/face/v1.0/detect")!)
request.HTTPMethod = "POST"
let postString = "entities=true&returnFaceId=true&returnFaceLandmarks=true"
let myData : NSData! = postString.dataUsingEncoding(NSUTF8StringEncoding)
let imageData :NSData = UIImagePNGRepresentation(image)!;
let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let body = NSMutableData();
let tempData = NSMutableData()
let fileName = "hello.jpg"
let mimeType = "application/octet-stream"
tempData.appendData(myData)
tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Disposition: form-data; name=\"\(fileName)\"; \(imageData)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData(imageData)
tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(tempData)
body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = body
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString!)")
}
task.resume()
}
XCPSetExecutionShouldContinueIndefinitely()
var image = UIImage(named : "male1.png")
//test()
imageUploadRaw(image!)
아래와 같은 걸로 테스트를 해봐도된다.
func test (){
let url = NSURL(string: "http://httpbin.org/get")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}
task.resume()
}
'모바일 > iOS' 카테고리의 다른 글
Swift2 - NSData to ByteArray (UInt8) (0) | 2016.06.06 |
---|---|
Swift2 - Using faceAPI and sending binary data to server (0) | 2016.06.06 |
Swift2 - Failed to obtain sandbox extension for (0) | 2016.06.06 |
Swift2- Reflection 사용법? (0) | 2016.06.06 |
[링크] Swift - SQLite 연동 (0) | 2016.06.06 |