2

I have a URL and want to download the image via a return function, however I cant get it to cooperate properly, here is my func:

func getTabImage(url: URL) -> UIImage {
    Alamofire.request(url)
        .responseImage { response in
            if let image = response.result.value {
                return image
            } else {
                print("Failed to get image")
            }
    }
}

I pass in the URL, and want a UIImage returned from the alamofire response.

But i get

Unexpected non-void return value in void function

for the return statement.

How can i achieve this correctly?

jackdm
  • 299
  • 5
  • 15
  • `return image` That won't work, because the call is asynchrone. Use a closure instead. There are already plenty of question, look for "Swift + Closure + Async". For the error message (which should disapear once you did the closure thing), it's because you said "-> UIImage", but in case of the else of `if let image` you don't return anything. Also, at the end of the method you don't return anything too. – Larme Sep 13 '17 at 13:44
  • Linked example is for URLSession but this is exactly the same idea. Add a callback to your method wrapper signature then use it with the value to retrieve (String in the example, but could be whatever). – Eric Aya Sep 13 '17 at 13:55
  • @Moritz he clearly asks **Downloading UIImage via AlamofireImage** that will not be marked duplicate with https://stackoverflow.com/questions/31264172/how-can-i-get-the-data-from-nsurlsession-sharedsession-datataskwithrequest may be it is duplicate but this question different with your marked question! – Salman Ghumsani Sep 13 '17 at 13:59
  • @SalmanGhumsani Can't you see that this is **exactly the same**?! Your answer is even the same thing I explain in the duplicate. It doesn't matter if Alamofire or URLSession, because the answer is the same: use a callback. – Eric Aya Sep 13 '17 at 14:00
  • @Moritz Okay its means `Alamofire` is not a library and it is not a different thing from `URLSession` Right? – Salman Ghumsani Sep 13 '17 at 14:03
  • @SalmanGhumsani A duplicate is about *answers*. The answer is the same: the question is marked as a duplicate. Simple as that. – Eric Aya Sep 13 '17 at 14:05
  • If you mark for closure reason so you should mark it as duplicate of the closure question! – Salman Ghumsani Sep 13 '17 at 14:05
  • @Moritz Also, simply we can see that you redirected it to your answer! – Salman Ghumsani Sep 13 '17 at 14:08
  • @Moritz Where you give your answer it already has as answer https://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift – Salman Ghumsani Sep 13 '17 at 14:15
  • 1
    I know you two I arguing. Personally I'm on @Moritz side. As I stated in my first comment, the real issue is link to the misunderstanding of async call. I suggested to author to look for "Swift + Closure + Async". ANY of the good answers on this kind of question should show the correct solution. I won't recommend one answer over another one, I expect all the authors to understand a little to adapt the solution to their code. – Larme Sep 13 '17 at 14:23
  • @Larme I don't have the faintest idea of why Salman is even arguing. I didn't downvote his answer (it's a good answer). The question being marked as duplicate is not a punishment to him or to OP. And yeah, absolutely, Alamofire or URLSession or else is totally irrelevant, the actual question is always the same (how to fetch value from async) and the already existing answers, mine or by other people, address the issue and can be used as duplicates. I hope Salman sees the light eventually... – Eric Aya Sep 13 '17 at 14:33
  • Okay I understand thanks, both of u also sorry for those arguing :( – Salman Ghumsani Sep 13 '17 at 14:38
  • 1
    @SalmanGhumsani Don't worry. It happens. People disagree or misunderstand, that's life. We're all different. No offense taken or given as far as I'm concerned. Have a good day, sir. :) – Eric Aya Sep 13 '17 at 14:42

1 Answers1

10

You can use the below function for downloading the image:

func getImage(_ url:String,handler: @escaping (UIImage?)->Void) {
        print(url)
        Alamofire.request(url, method: .get).responseImage { response in
            if let data = response.result.value {
                handler(data)
            } else {
                handler(nil)
            }
        }
    }

Uses

getImage("http://") { (image) in
    if image != nil {
        print(image)
    }
}

Or

If you want to set the image on UIImageView use extension of AlamofireImage.

if let imageURL = URL(string: "http://"), let placeholder = UIImage(named: "default") {
     imageView.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete.
}
Salman Ghumsani
  • 3,491
  • 1
  • 17
  • 32