Search code examples
iosalamofireuitableviewalamofireimage

iOS 9 Alamofire never loads image


I'm using AlamofireImage for loading images into an ImageView in a tableview cell (in a separated xib file) The problem is that the image never shows up. I think the code is right and url is valid too.

Here is the code (very simple):

let placeholderImage = UIImage(named: "imgNoPhoto1")    
if let urlImage = NSURL(string: urlString) {
      photoImage.af_setImageWithURL(urlImage, placeholderImage: placeholderImage)
    }

Any ideas? Could be that the cell is not being reloaded? I have tested it in iOS 8 and 9.

Hope you help me! Thank you


Solution

  • Depending on whether you're cancelling the request in prepareForReuse or not, you may be running into a bug that I just fixed in AlamofireImage #55. I'll be pushing out a new release with this fix here in the next couple of days. If this is actually what you're running into, you could comment out the cancellation logic for now and that should fix your problem until we release the fix.

    If this is not the issue you are running into, then I'd follow the advice of everyone else and make sure you can download the image using cURL.


    Update #1

    Okay, I found out what your issue is. The server is not returning a valid content type which is causing AlamofireImage to not validate the image and it won't try to decode the data into an image. You can find this by running the following command in Terminal:

    curl -H "User-Agent: iOS" -s -D - http://files.encuentra24.com/normalsq/sv/58/08/68/sv/58/08/68/5808689_5e7d99.jpg -o /dev/null
    

    What this does is run curl against the URL you provided. It doesn't download the image data, it just prints out the response headers. I also found that you need to pass the User-Agent header, otherwise you'll always get a 403. Here's what the curl command will print out:

    cnoon:~$ curl -H "User-Agent: iOS Example/com.alamofire.iOS-Example (1; OS Version 9.1 (Build 13B137))" -s -D - http://files.encuentra24.com/normalsq/sv/58/08/68/sv/58/08/68/5808689_5e7d99.jpg -o /dev/null
    HTTP/1.1 200 OK
    Cache-Control: max-age=2592000, public
    Content-Type: image/jpg
    Date: Fri, 11 Dec 2015 16:16:38 GMT
    Expires: Sun, 10 Jan 2016 16:16:38 GMT
    Pragma: no-cache
    Server: nginx/1.7.12
    Set-Cookie: sessioninfo=uv491lgtjqvkmt267l1nmlbm24; path=/
    Set-Cookie: esid=deleted; expires=Thu, 11-Dec-2014 16:16:37 GMT; path=/
    Vary: Accept-Encoding
    Content-Length: 23757
    

    Now the REALLY important part of this output is the Content-Type: image/jpg. That's not actually a valid Content-Type header. The valid one is image/jpeg. Therefore, AlamofireImage by default won't validate this response and won't decode the image.

    Solution

    Thankfully, we already have support for this built into AlamofireImage. You can add a custom content-type to the Request response serializers. How you do that is as follows:

    Alamofire.Request.addAcceptableImageContentTypes(["image/jpg"])
    

    This will register the image/jpg content type as an acceptable content type with the response serialization system. After registering, any content type matching image/jpg will be decoded. For more info about this, please refer to AlamofireImage #58.