2

我尝试过使用 Alamofire 和 alamofireimage,但似乎无法始终如一地获得下载图像的文件大小。我使用了错误的方法吗?因为如果 Alamofire 已经下载了图像,它肯定会知道文件大小是多少?

这是我正在使用的示例代码

 Alamofire.request(.GET, imageUrlToShow)

        .responseImage { af_image in
            debugPrint(af_image)

            print(af_image.request) // Sometimes Content-Length is returned by other times not

            if af_image.result.isFailure {
                print("error")
                completionhandler(imageInfo: nil, error: af_image.result.description)
            }
            if af_image.result.isSuccess {

                if let serverResponse = af_image.response {
                let serverHeaders = serverResponse.allHeaderFields

                    if let imageSize = serverHeaders["Content-Length"] as? String {
print("we got an imagesize")
}
4

2 回答 2

1

这是最新版本的最后一个正确语法AlamofireImage2.4.0 与Alamofire3.3 的依赖项):

import AlamofireImage

var ext: String! = "jpeg"

self.ext = "png"
Alamofire.request(.GET, "https://httpbin.org/image/png")
         .responseImage { response in
             debugPrint(response)

             print(response.request)
             print(response.response)
             debugPrint(response.result)

             if let image = response.result.value {
                 print("image downloaded: \(image)") // example: background.png
                 var imgData: NSData!
                 switch self.ext {
                    case "png":
                      imgData = NSData(data: UIImagePNGRepresentation(image)!)
                    case "jpeg":
                      imgData = NSData(data: UIImageJPEGRepresentation((image), 1)!)
                    default:
                      break
                 }
                 if let data = imgData {
                     print("Size of Image: \(data.length) bytes")
                 }
             }
         }

您还可以使用一般的Alamofire框架,正是这个示例(下载文件或恢复已经在进行中的下载)。

Alamofire.download(.GET, "https://httpbin.org/stream/100") { temporaryURL, response in
    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let pathComponent = response.suggestedFilename

    return directoryURL.URLByAppendingPathComponent(pathComponent!)
}

Alamofire.download(.GET, "https://httpbin.org/stream/100", destination: destination)
         .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
             print(totalBytesRead)

             // This closure is NOT called on the main queue for performance
             // reasons. To update your ui, dispatch to the main queue.
             dispatch_async(dispatch_get_main_queue()) {
                 print("Total bytes read on main queue: \(totalBytesRead)")
             }
         }
         .response { _, _, _, error in
             if let error = error {
                 print("Failed with error: \(error)")
             } else {
                 print("Downloaded file successfully")
             }
         }
于 2016-07-14T12:44:42.737 回答
0

您可以使用.responseData()来获取返回的数据,并从中获取大小。不幸的是,准确性Content-Length取决于服务器返回的正确信息。正如您所见,这并不总是一个准确的指标,也不总是包含在响应中。

于 2016-07-14T18:20:07.993 回答