jordy
jordy

Reputation: 31

Get size of an image placed on remote server before downloading

I'm trying to get the size in Ko of an image which is placed in a remote server and check this to know if it is necessary to download it. I tried a lot of examples i found in this forum but nothing work for me with Xcode 8 and swift 4. First, i try to get the header like this:

func getHeader() {
    for (index, item) in imgUrlArray.enumerated() {
        let session = URLSession.shared
        session.dataTask(with: item) {
            (data, response, error)->Void in

            if let responseData = data {
                do {
                    let json =  try

                    JSONSerialization.jsonObject(with: responseData,
                        options: JSONSerialization.ReadingOptions.allowFragments)
                    print(json)
                } catch {
                    print("Could not serialize")
                }
            }
        }.resume()
    }
}

imgUrlArray is an array with remote URLs like: http://www.test.com/image.jpg

In this case data is nil. Where is my mistake?

Upvotes: 0

Views: 284

Answers (2)

jordy
jordy

Reputation: 31

i have found this solution after few days of search:

    func getHeaderInformations (myUrl: URL, completion: @escaping (_ content: String?) -> ()) {

    var request = URLRequest(url: myUrl)
    request.httpMethod = "HEAD"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    guard error == nil, let reponse = response as? HTTPURLResponse, let contentType = reponse.allHeaderFields["Content-Type"],let contentLength = reponse.allHeaderFields["Content-Length"]

        else{
            completion(nil)
            return
    }
        let content = String(describing: contentType) + "/" + String(describing: contentLength)

            completion(content)
    }
    task.resume()
}

Usage is like this:

getHeaderInformations(for: url, completion: { content in

print(content ?? 0)

})

I hope this answer could help someone.

Upvotes: 1

Nate
Nate

Reputation: 2404

You should use that code example of the HEAD request that you posted, but instead of using two functions and two web requests, make 1 function called getHeaders that returns request.allHeaderFields. Then you can make a method that calls getHeaders and if the Content-Length and Content-Type are what you expect, then perform a GET request to actually download the data.

This approach would be more efficient for the user and the server because they’d only do 1 HEAD request instead of 2.

Upvotes: 0

Related Questions