I am making an app that works as a feed reader.
I have a UITableViewController
, in which I show all the news that I read from an xml, I show the title, the date, a brief description and an image. Except the image, everything I show is correct.
I have added the library AlamofireImage
to display the images. At first they don't appear, but if I reload the view with a reloadData()
, the images appear.
Most of the images are seen in their correct position, but others are out of position and in some, only image is displayed. The title, date and description do not appear.
If I put a fixed width and height on the images, it appears perfectly, but if I only position them by constraints, it is when the error appears, that they take time to load
Here is the code I use:
if let actualImageView = imageView {
// actualImageView.load(currentArticleToDisplay.imagen)
/*let url:NSURL? = NSURL(string:currentArticleToDisplay.imagen)
let imageRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(imageRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {(response, data, error) in
actualImageView.image = UIImage(data: data!)
//actualImageView.contentMode = UIViewContentMode.ScaleAspectFit
})*/
actualImageView.contentMode = .ScaleAspectFit
let URL = NSURL(string: currentArticleToDisplay.imagen)!
actualImageView.af_setImageWithURL(
URL,
placeholderImage: nil,
filter: nil
)
//actualImageView.frame = view.bounds
//actualImageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
//actualImageView.contentMode = UIViewContentMode.ScaleAspectFit
}
I have the constraints defined in the Main.storyboard
, because if I put them in code, they didn't load me at first either.
Any idea what could be the problem? Do I need to add more parts of the code? Is this the correct way to display images? Is there a library that can fix it?
Thank you!
Using
AlamofireImage
is correct, only that you are not taking into account that the requests are made asynchronously ; that's why the images 'don't show up' the first time , since they haven't finished loading yet when you invoke:Which obviously returns
nil
and is not done until you run againreloadData()
, which on the other hand, you don't even need to manually update the table withreloadData()
since itAlamofireImage
updatesUIImageView
when it has finished downloading.I am not sure where you are assigning the image so I will assume that you do it in the delegate method
cellForRowAtIndexPath
, so I share the following sample code that does what you want with Dummy content :When the images finish downloading, they will be displayed in the assigned place. You could also substitute:
by
Where
"placeHolder.png"
is an image that you would have in your project and that will be displayed while the real images finish downloading.It is important to know that
UItableView
cells reuse them and therefore it is important that the instance ofUIImageView
should not be the same. The correct way is that you implement the delegate method like so: