IOS - SWIFT - CollectionView With images, loading URLs from array - ios

I have a collection view and array with URLs of different images. and when i launch the app, collection view starts to load images through the array:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellView
var url = arr[indexPath.row]
var urls = NSURL(string: url)
var data = NSData(contentsOfURL: urls!)
cell.ImageView.image = UIImage(data: data!)
return cell
}
and the trouble appearse:
for example on 4th cell collection view loading all 4 urls for all 4 cells and it takest alot time. how can collection view load particular url for particular cell and don't spend time to load urls to cells that already loaded?
Thanks for any help!!

I suggest using a third party library for this matter, it called SDWebImage.
And than for each image view inside a cell set:
self.imageView.sd_setImageWithURL(url, completed: block)

Or you can use similar third party library, as Asaf, told you. I used to use HANEKE for DL/cache images.
Take a look: https://github.com/Haneke/HanekeSwift
:)

Related

How to fill images in to UICollectionView with Alamofire Swift 4

I have a Chat Log which is simply a UICollectionView. Every sell has an avatar (UIImage) and a text bubble. I'm trying to fill avatars with proper images by fetching avatars URL from the server with Alamofire like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chatMessage", for: indexPath) as! ChatMessageCell
let imageURL = URL(string: messagesArray![indexPath.item].userAvatar)
Alamofire.download(imageURL!).responseData { response in
if let data = response.result.value {
cell.userAvatar.image = UIImage(data: data)
}
}
cell.messageText.text = messagesArray![indexPath.item].userText
}
My problem: In some cells avatar appears in some is not. I think it's related to Alamofire async work. So my question is how to fill images to UICollectionView properly to show each avatar in each cell?
I think it's better to use SDWebImage , as according to your current implementation image fetching happens multiple times
imageView.sd_setImage(with: URL(string: "http://www.example.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
here SDWebImage
UICollectionView reuses existing cells. If you have a collectionView with one million cells there are not going to be one million UICollectionView cells instantiated but they got reused when they scroll out. Now your result callback blocks may set the image of the same cells.
To solve your problem you need to cache the images. There are multiple tutorials about this topic.

How Do I Prevent Reload of UICollectionViewCells in cellForItemAt?

I have a CollectionViewController in which I contact an API to download images based on a set of coordinates. I call this code in the cellForItemAt function at which time it updates the cell's images in realtime with images from Flickr. This works fine.
However, when scrolling up or down, it recalls this code and updates the cells again, when I'd prefer that it look at the existing cells, identify if they have been filled, and simply not run this code.
I have tried implementing logic before the networking code that checks to see if the imageView.images already exist in a local struct I assign them to, but that doesn't seem to work correctly.
Is there a simple method to tell cellForItemAt "for cells where you already have images, don't look for more"?
Here is my current code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath as IndexPath) as! CollectionViewCell
// Get images = using the URL
FlickrClient.sharedInstance().getImagesFromFlickr(latitude: selectedPin.lat, longitude: selectedPin.lon, page: pageCount) { (pin, error) in
if let pin = pin {
let url = pin.images[indexPath.item].imageURL
let data = try? Data(contentsOf: url)
performUIUpdatesOnMain {
cell.imageView.image = UIImage(data: data!)
cell.imageView.contentMode = .scaleAspectFill
}
}
}
return cell
}
Use SDwebImage libray for loading images from url.
https://github.com/rs/SDWebImage
Call Something like this on cell for row :
let url = pin.images[indexPath.item].imageURL
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"))

Slow loading of images in UITableViewController extracted from URL string

I'm trying to load images extracted from the web URL into the image view of each cell.
However, when i scroll the table the screen will freeze as I believe it is attempting to grab the images for each cell 1 by 1.
Is there a way i can make it asynchronous? The resources available out there currently is outdated or incompatible(running obj c) as I'm running on Swift 2
The relevant code I'm using within the table view controller is below :
override func tableView(newsFeedTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let blogPost: BlogPost = blogPosts[indexPath.row]
cell.textLabel?.text = blogPost.postTitle
let unformattedDate = blogPost.postDate
//FORMATTING: Splitting of raw data into arrays based on delimiter '+" to print only useful information
let postDateArr = unformattedDate.characters.split{$0 == "+"}.map(String.init)
cell.detailTextLabel?.text = postDateArr[0]
let url = NSURL(string: blogPost.postImageUrl)
let data = NSData(contentsOfURL: url!)
cell.imageView!.image = UIImage(data: data!)//WHY SO SLOW!?
print(blogPost.postImageUrl)
return cell
}
Try this
var image: UIImage
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in
// Background thread stuff.
let url = NSURL(string: blogPost.postImageUrl)
let data = NSData(contentsOfURL: url!)
image = UIImage(data:data)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
// Main thread stuff.
cell.imageView.image = image
})
})
Lets clean your code a bit. First of all, you are trying to declear ALL your cells in your viewController. That means your app is not trying to load every image one byt one, but more like everything all together.
You need to create a separate file called PostCell what is going to be a type of UITableViewCell.
Then you need to go to your prototype cell and connect those view elements to that PostCell just like you would add those to any other ViewController.
Now, here is new code to your cellForRowAtIndexPath function:
override func tableView(newsFeedTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let blogPost = blogPosts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? PostCell {
cell.configureCell(blogPost)
return cell
}
return UITableViewCell() // You need to do this because of if let
}
And declear this on that PostCell:
func configureCell(post: BlogPost) {
this.textLabel.text = post.postTitle
let postDateArr = unformattedDate.characters.split{$0 == "+"}.map(String.init)
this.detailTextLabel.text = postDateArr[0]
// I would add few if let declarations here too, but if you are sure all these forced ! variables do exciest, then its ok
let url = NSURL(string: blogPost.postImageUrl)
let data = NSData(contentsOfURL: url!)
this.imageView.image = UIImage(data: data!)
}
Or something along those lines. When you connect those elements to your cell, you will get proper variable names for those.
That SHOULD help. There are plenty of tutorials how to make a custom tableviewcell. Some of them advice to put all the declarations inside that cellForRowAtIndexPath, but I have found that it get's problematic very fast.
So...my advice in a nutscell...create a custom tableviewcell.
Hope this helps! :)
To load the image on every cell use SDWebImage third party library. You can add it using pods as put pod 'SDWebImage' It provides various methods to load the image with caching or without caching asynchronously. With caching you don't really need to worry about loading image data every time cell appears on the screen. Try this
override func tableView(newsFeedTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? PostCell {
--reset your cell here--
// cell.imageView.image = nil
}
cell.imageView.sd_setImageWithURL(YOUR_URL, placeholderImage: UIImage(named: "")) {
(UIImage img, NSError err, SDImageCacheType cacheType, NSURL imgUrl) -> Void in
// Do awesome things
}
-- configure your cell here --
}

Populating UITableView with JSON data from youtube videos in Swift 2 and Xcode 7

I'm trying to build an app that is basically a youtube player.
It has a UITableView where JSON data will be parsed from youtube according to the user's search (in the search bar) and present it on the cells with the video image, title and a small description.
If the user clicks on the cell a new view opens and shows the video player where the user can play/stop/scrub and see more details about the video.
My question is how to exactly populate the cells with this data and how to link the next view with it.
Someone has any idea how to do this? Can you please show me the code on how to do it, or give me some advice on where to find the answer?
Thanks in advance! :)
You first have to create a UITable and implement the delegate methods together with a custom class that will act as cell for the UITable if you dont want the the deafult behaviour of the class.
For passing the data to the details you will use something called the segue you can have a look at the below project on github.
As for getting the data from youtube you will probably get a json response from youtube which you have to decode and add the objects to a list. You then just assign the object to the coressponding element like the below.
//the view of every cell of the list
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//custom cell identifier
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ViewControllerCell
//setting up the images and casting them from strings to UIImages
let imageURL = NSURL(string: JSONGameImages[indexPath.row])
let data = NSData(contentsOfURL: imageURL!)
//setting game name, images and viewers on cells
cell.gameName?.text = JSONGameName[indexPath.row]
cell.gameViewers?.text = String (JSONViewers[indexPath.row])
cell.gameImage?.image = UIImage(data: data!)
return cell
}
https://github.com/renegens/Greenely

UITableView displaying webImages in Swift languages seems to freeze

so i'm kind of a noobe to iOS and Swift also (actually only one month and a half). So I'm trying to make a tableview displaying some images coming from the web, I managed to get the correct info from a web API and display them in the tableview. But, the tableview with online images seems to freeze, I should wait 2-3 seconds for it to move just a little bit and freeze again, which is very frustrating.
I know it has something to do with synchronize, I also tried dispatch_async(dispatch_get_main_queue()){}, and since I'm developing with Swift not ObJ, I tried an open source Web Image download helper from Github called Kingfisher, but it didn't solve my problem.
So, please, help me, and thanks.
To keep it simple, here's the demo code:
var arrays = [
"NPWR02174_00_013F09F4AE3D70230FA695261A8E994D275DF62333/C878A05EF327D7D42E2AF5E1EE15672A1509962C.PNG",
"NPWR04270_00_01C53CAC0BACAC6F052AE4B968370CF9B899DDA81E/C1E1A8AD62EDE2FB95C3BD562F2783F157A0D011.PNG",
"NPWR04472_00_01CB90FF28A2EBB55D210932F56935B377E7A28ECB/DCA2F66365777A7330F685CD8CD5F90F2FE62671.PNG",
"NPWR04841_00_017B982A2A44BB607DECD77484C4670CCB505B65E4/91824E16D16C2789CC34A2F5F5444CD30D71C8F1.PNG",
"NPWR05212_00_018ADD99CEA95C240EFDC92FB993F6BC8C0AF7904D/7266E29159EE69D31AA24FB94BC0E90F5306774A.PNG",
"NPWR05254_00_015EB28BB38A5580D6A73CA0BDFB2F8C6864F85F66/76ED779ECE98E63E8A1A229AE626F62E8325D703.PNG",
"NPWR05257_00_01CB48AFF2A9F0795B5569E1DABBC612E3FA13B79A/3524288D6B1E5B4F51CF7DEA4A3E621913A1BA8C.PNG",
"NPWR05326_00_01DF67B968910B06FDABD8A2C2C6AAB72E0DD47048/8AB6EC3EC992C35472F02DD0C8D4122302C802E1.PNG",
"NPWR05401_00_0188285B6025E65909B39E295D2542DF589EDCCC38/620822A98E3CFC1CFB3B5EDCF3F00B19A5B328C2.PNG",
]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrays.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCellWithIdentifier("gameCell")!
dispatch_async(dispatch_get_main_queue()){
let image = cell.viewWithTag(101) as! UIImageView
let imageString = arrays[indexPath.row] as! String
let imageURL = NSURL(string: "https://trophy01.np.community.playstation.net/trophy/np/"+imageString)!
let data = NSData(contentsOfURL: imageURL)!
image.image = UIImage(data: data)}
return cell
}
It is because you are downloading the image synchronously. This is the code line that does so.
let data = NSData(contentsOfURL: imageURL)
Try downloading the image asynchronously and then update the cell once image is downloaded. Checkout this answer How to async load images on dynamic UITableView?

Resources