4

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

Marta Tenés
  • 2,102
  • 1
  • 13
  • 22
  • did you see placeholder image or nothing ? – sage444 Dec 10 '15 at 14:24
  • Did you checked the type of image you getting from the url? Your code seems to be alright but image type might be the problem. – Vijay Sanghavi Dec 10 '15 at 14:25
  • @sage444 yes, the placeholder is always showed. It is supposed that when the image is loaded, the placeholder must be replaced by the image but is not working... – Marta Tenés Dec 10 '15 at 14:26
  • @VijaySanghavi it's a jpg. Here is one of the url I receive: http://files.encuentra24.com/normalsq/sv/58/08/68/sv/58/08/68/5808689_5e7d99.jpg – Marta Tenés Dec 10 '15 at 14:28
  • please check this url with curl `curl -v "http://files.encuentra24.com/normalsq/sv/58/08/68/sv/58/08/68/5808689_5e7d99.jpg"` – sage444 Dec 10 '15 at 14:32
  • @sage444 How can I use cURL in Swift? I'm getting the url (and other data) from a get request method... – Marta Tenés Dec 10 '15 at 15:02
  • @MartaTenés oh, no, I mean you should check server response with curl, not use it in code:)) – sage444 Dec 10 '15 at 15:04

1 Answers1

5

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.

cnoon
  • 16,575
  • 7
  • 58
  • 66
  • Hello @cnoon thanks for your answer. I'm simply doing a get request from the view controller which has the table view and then in cellForRowAtIndexPath I pass the url to the cell. (cell is in a separated xib) Maybe the problem is that I'm not setting up the image in the correct cell's lifecycle method. – Marta Tenés Dec 11 '15 at 07:43
  • Thank you so much! I added this line Alamofire.Request.addAcceptableImageContentTypes(["image/jpg"]) before doing af_setImageWithURL and now photos are shown!! You saved my day :) – Marta Tenés Dec 14 '15 at 07:33
  • Awesome...glad to hear it! – cnoon Dec 14 '15 at 14:39