AlamoFire:如何在swift中获取下载图像的文件大小?

时间:2016-07-14 11:03:26

标签: swift alamofire alamofireimage

我尝试过使用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")
}

2 个答案:

答案 0 :(得分:0)

这是最新版本(AlamofireImage 2.4.0的最后一个正确syntax,依赖于Alamofire 3.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")
             }
         }

答案 1 :(得分:0)

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