Tôi đã cập nhật mã của mình lên Swift 3 và đang gặp sự cố khi di chuyển sang Alamofire 4.0. Tôi đã sử dụng hướng dẫn di chuyển Alamofire để thực hiện thành công hầu hết các sửa đổi cần thiết, nhưng vẫn gặp sự cố khi nhận dữ liệu hình ảnh.Nhận dữ liệu hình ảnh với yêu cầu Alamofire
Các cũ Swift 2/Alamofire 3 mã (làm việc như dự định):
func beginGetImageRequest() {
if let imagePath = thumbPath {
request = Alamofire.request(.GET, imagePath).response(completionHandler: { (_, _, imageData, error) -> Void in
if error != nil {
NSLog("Error downloading thumbnail image: \(error)")
} else {
if let downloadedImage = UIImage(data: imageData!) {
self.imageView.image = downloadedImage
}
}
})
}
}
nỗ lực của tôi tại cập nhật lên Alamofire 4:
func beginGetImageRequest() {
if let imagePath = thumbPath {
request = Alamofire.request(imagePath, method: .get, parameters: [:], encoding: JSONEncoding.default)
.validate { request, response, imageData in
if let downloadedImage = UIImage(data: imageData!) {
self.imageView.image = downloadedImage
} else {
print(response)
print(imageData)
}
return .success
}
}
}
print(imageData)
đầu ra Optional(306 bytes)
. Hình ảnh nên có kích thước khoảng 40 kb, cho tôi biết vấn đề là với cách tôi đang thực hiện yêu cầu, không phải với cách tôi chuyển đổi dữ liệu sang UIImage.
Đây là kết quả cho print(response)
<NSHTTPURLResponse: 0x618000221660> { URL: http://209.126.98.238/cache/igames_thumb/images/games/53848027743af.jpeg } { status code: 400, headers {
Connection = close;
"Content-Encoding" = gzip;
"Content-Length" = 245;
2016-10-04 21:54:53.653480 EyeGames[74216:3416747] [] nw_connection_send_stats_report 21 Generated report:
Delegated: 0
Report reason: app data stall
TCP statistics report:
Time to DNS start: 0 ms
Time to DNS resolved: 0 ms
DNS resolved time: 0 ms
DNS answers cached: 0
Interface type: 1
Time to TCP start: 3 ms
Time to TCP establishment: 223 ms
Connection establishment: 220 ms
Flow duration: 11447 ms
Connected interface type: 1
Connected: 1
Traffic class: 0
Cellular fallback: 0
Cellular RRC connected: 0
Kernel reported stalls: 0
Kernel reported connection stalls: 0
Kernel reported read stalls: 0
Kernel reported write stalls:
"Content-Type" = "text/html; charset=iso-8859-1";
Date = "Tue, 04 Oct 2016 18:54:43 GMT";
Server = "Apache/2.2.22 (Debian)";
Vary = "Accept-Encoding";
} }
Và những gì phản ứng chứa? bạn nên bắt đầu ở đó – Godfather
@Godfather Đã chỉnh sửa câu hỏi của tôi để bao gồm đầu ra từ phản hồi –
Bạn đang nhận được 400 mã lỗi. Yêu cầu của bạn không đúng định dạng. Hãy thử thay đổi mã hóa thành URL – Godfather