2

I am trying to downlaod an image.

This is the link of the image

https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png

what I did is:

   let url = NSURL(string: "https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
        let session = NSURLSession.sharedSession()
        let task = session.downloadTaskWithURL(url!, completionHandler: {(url, response, error) in
            if let error = error {
                print("error = \(error)")
            }

            if let response = response {
                print("response = \(response)")
            }

        })
        task.resume()

really i have a response, and i can see it in the log as the following:

response = <NSHTTPURLResponse: 0x7fc1a1427060> { URL: https://www.google.it/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png } { status code: 200, headers {
    "Cache-Control" = "private, max-age=31536000";
    "Content-Length" = 13504;
    "Content-Type" = "image/png";
    Date = "Mon, 23 Nov 2015 23:57:55 GMT";
    Expires = "Mon, 23 Nov 2015 23:57:55 GMT";
    "Last-Modified" = "Fri, 04 Sep 2015 22:33:08 GMT";
    Server = sffe;
    "alt-svc" = "quic=\"www.google.com:443\"; p=\"1\"; ma=600,quic=\":443\"; p=\"1\"; ma=600";
    "alternate-protocol" = "443:quic,p=1";
    "x-content-type-options" = nosniff;
    "x-xss-protection" = "1; mode=block";
} }

my problem is that there is no data in the response to get the actual image. I used to call the dataTaskWithRequest and the clouser for it have a data like this:

session.dataTaskWithRequest(request, completionHandler: {(data, response, error)

but here i didn't find the data, what is my wrong please?

sarah
  • 1,201
  • 1
  • 9
  • 29

1 Answers1

0

When using a "download" task, you need to implement a session delegate method to get the data. Download tasks put the data into a temporary file, then call the delegate to tell it where that file is located.

If you implement URLSession:downloadTask:didFinishDownloadingToURL: in your download delegate, that method will be called with a file URL pointing to the temporary file.

If the images aren't too big, you can use data-style tasks instead. They download the data to memory, then give you an NSData object in the completion handler.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • sound so good, but please how can i add that delege? – sarah Nov 24 '15 at 00:18
  • i understand that i have to make a class that adopt to a protocol ,and i have to write the function `did finish download to url` right? if yes what is the protocl and how to tell the session to use it ? – sarah Nov 24 '15 at 00:19
  • `NSURLSession` has a property called `delegate`, which can be any object that adopts the `NSURLSessionDelegate` protocol. You need to have an object that adopts the protocol and assign it to that property. – Tom Harrington Nov 24 '15 at 00:20
  • there is no `didFinishDownloadingToURL` function inside the `NSURLSessionDelegate ` protocol. is it the correct method ? – sarah Nov 24 '15 at 00:26
  • The [documentation for `NSURLSessionDelegate`](https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSURLSessionDelegate_protocol/) explains that *"In addition to the methods defined in this protocol, most delegates should also implement some or all of the methods in the NSURLSessionTaskDelegate, NSURLSessionDataDelegate, and NSURLSessionDownloadDelegate protocols to handle task-level events."* – Tom Harrington Nov 24 '15 at 00:28
  • are you saying that i should also conform to these three new protocols? (thanks for your understanding i am new to swift, and to ios in general ) – sarah Nov 24 '15 at 00:30
  • sounds like the delegate property in the sesion is a read only, and i can't assign values to it. – sarah Nov 24 '15 at 00:38
  • My mistake, you should be using `sessionWithConfiguration:delegate:delegateQueue:` when you create the session object. – Tom Harrington Nov 24 '15 at 00:39
  • can you give me a simple code please? sounds like you are an expert, and i am still in defining my class `class imageDelegate: NSURLSessionDownloadDelegate { @objc func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) { print("asdfasf") } }` with a lot of errors – sarah Nov 24 '15 at 00:44
  • even this sesionWithConfiguration, i dind't know where to add it inside my let session = NSURLSession.sharedSession(), i tried to sounds like this method is in another place – sarah Nov 24 '15 at 00:46
  • I would suggest this Objective-C example as a starting point: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW14 – dgatwood Dec 03 '15 at 21:22