I have data coming from Json and populating it inside a TableView . One of those elements that I am getting back from Json is a String that has a URL to an image . If a particular Json string is null or blank I get the following error
fatal error: Index out of range
Every post will not have an Image but I do not know how to tell swift to ignore a certain section of code if the String is blank . This is my code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! HomePageTVC
cell.post.text = Posts[indexPath.row]
// Start Display Image
// right here I get the fatal out of index if there is no image
if profile_image_string[indexPath.row] != nil {
let imgURL: NSURL = NSURL(string: profile_image_string[indexPath.row])!
let request:NSURLRequest = NSURLRequest(url: imgURL as URL)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
DispatchQueue.main.async(execute: { () -> Void in
if data != nil {
cell.profile_image.image = UIImage(data: data!)
}
})
});
task.resume()
}
// End Display Image
return cell
}
This is my Json being returned
{"post":"Check check","profile_image":null},
{"post":"check check","profile_image":"http://www.myurl.com/cats.jpg}
notice that if no image url exist then the default value is null . How can I check for that inside my TableView code ? Because what I am sure that is happening is that it's taking that null value and trying to convert it into an image thus I am getting the error. The value of the string URL is kept inside this string Array
profile_image_string[indexPath.row]
and this is how I append it
if let profile_picture = Stream["profile_image"] as? String {
self.profile_image_string.append(profile_picture)
}
anyways as stated before I am successfully getting the URL String from Json and if it is a URL then the image shows, I just want to know how can I check for Nulls that way I stop getting that error any help would be great .
This part is pretty wrong
if let profile_picture = Stream["profile_image"] as? String {
self.profile_image_string.append(profile_picture)
}
If you want consistent array value, you have to make your profile_image_string can contains nil by make it's type [String?] and if the if let fail, append nil value into the array like:
if let....else {
self.profile_image_string.append(nil)
}
Still, this way is very messy and not advised, i suggest you create proper object to hold your JSON data
Related
I want to load my images from Firebase to my Table View but I get the error:
Cannot convert value of type 'String' to expected argument type 'URL'
When I print the object on its own it is definitely a URL.
This is what my code looks like:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedItem", for: indexPath) as! FeedItem
//TODO: Guard...
let postImage = postArray [indexPath.row]
let postImageURL = postImage.postImageURL
let data = Data(contentsOf: postImageURL) // Line with Error
cell.postImage.image = UIImage (data: data)
return cell
}
To display the image in your cell, you need to convert the URL string into an actual URL object, which you can do via:
let postImage = postArray[indexPath.row]
if let postImageURL = URL(string: postImage.postImageURL)
{
do {
let data = try Data(contentsOf: postImageURL)
cell.postImage.image = UIImage (data: data)
} catch {
print("error with fetching from \(postImageURL.absoluteString) - \(error)")
}
}
And as rmaddy implies, your performance is not going to be very good (because depending on how far away the remote server is or how slow the internet is), the synchronous "Data(contentsOf:" call might take an unacceptably long time to succeed. I'm just providing this answer so you will be able to see something in your own testing, but I wouldn't use this in production code.
Try to replace the Data fetch with an asynchronous URLSession task, and you can find much more information in this very related question.
I am creating a social network app in order to learn swift (using Swift 4) and complete a project . I have a TableView that shows videos and I have added the functionality of 'liking' videos like any social network. My issue is that when you like a video the TableView gets reloaded to show the '+ 1 like' and the Video starts all over again. How can I make it so that the video doesn't restart every time you like a video . This is my code here
1st You get the user clicking the Like Action which sends a call to the database and insert the like and add '+1 to the like field'
#IBAction func LikeAction(_ sender: UIButton) {
DontReload = sender.tag
let url:URL = URL(string:ConnectionString+"insert_like")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let parameter = "parameters"
request.httpBody = parameter.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(String(describing: error))")
return
}
DispatchQueue.main.async {
self.reloadTable()
}
}.resume()
}
Then I query the database and return the new data showing the +1 like and other data in Json Format.
func reloadTable() {
var url = URL(string:ConnectionString+"streams")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let parameter = "Parameters"
request.httpBody = parameter.data(using: String.Encoding.utf8)
session.dataTask(with:request, completionHandler: {(data, response, error) in
if error != nil {
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
if let Streams = parsedData["Results"] as? [AnyObject]? {
// check for misspelled words
if streamsModel.Locations.count >= 0 {
// Set My Arrays
}
for Stream in Streams! {
// Gets Json Values
}
TableSource.reloadData()
}
}
else {
DispatchQueue.main.async {
streamsModel.Locations.removeAll()
TableSource.reloadData()
}
} catch let error as NSError {
print(error)
}
}
}).resume()
}
This is my TableViewCell and this is obviously called to show the new updated data, however if the user is watching a video and likes it while it is playing then the video restarts... any suggestions on solving this would be great.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
// Starts Video Plays
cell.videoView = cell.VideoViewArray[indexPath.row]
cell.videoView.tag = indexPath.row
let movieURL = URL(string: cell.stream_image_string[indexPath.row])
cell.videoCapHeight.constant = CGFloat(Float(cell.pic_height!))
cell.playerView = AVPlayer(url: movieURL!)
cell.MyAVPlayer.player = cell.playerView
cell.MyAVPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
cell.MyAVPlayer.showsPlaybackControls = false
cell.MyAVPlayer.view.frame = cell.videoView.bounds
cell.videoView.addSubview(cell.MyAVPlayer.view)
controller.addChildViewController(cell.MyAVPlayer)
cell.playerView?.isMuted = false
cell.MyAVPlayer.player?.play()
// Ends Video play
return cell
}
Again my code works the only issue is that my videos restart on Table Reloads . I want to create some type of condition or flag that If a Table View Cell gets 'liked' and there is a video then I want that video to not get reloaded . Any suggestions would be great . Based on suggestions below I will stop using ReloadTable and attempt to grab a reference for that UIButton perhaps something like this
let indexPath = NSIndexPath()
let cell = self.TableSource.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath as IndexPath) as! HomeTVC
cell.votes.setTitle("result from server",for: UIControlState.normal)
As per your requirement is seems you don't need to reload whole table view in case of just update like, Once you receive API response of "insert_like" You can update your array and directly get reference of your video running cell and update data source of it.
Title says everything. I'm just unable to download an image from Firebase Storage dir. Here is the snippet of the code which calls the function for setting data and it also calls the function which tries to download the picture:
for element in Dict {
if let itemDict = element.value as? [String:AnyObject]{
let name = itemDict["name"] as! String
let price = itemDict["price"] as! Float
let imageObject = itemDict["image"] as! NSDictionary
let hash = imageObject["hash"] as! String
let storageDir = imageObject["storageDir"] as! String
let image:UIImage = self.downloadImageProductFromFirebase(append: hash)!
let product = Product(name: name, image: image, imageName:hash, price: price, storageDir : storageDir)
self.productList.append(product)
}
}
print(Dict)
self.myTable.reloadData()
And here is the code which tries to download the image:
func downloadImageProductFromFirebase(append:String) -> UIImage?{
let gsReference = Storage.storage().reference(forURL: "gs://fridgeapp-3e2c6.appspot.com/productImages/productImages/" + append)
var image : UIImage?
gsReference.downloadURL(completion: { (url, error) in
if error != nil {
print(error.debugDescription)
return
}
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error.debugDescription)
return
}
guard let imageData = UIImage(data: data!) else { return }
DispatchQueue.main.async {
image = imageData
}
}).resume()
})
return image
}
But, for some reason, it crashes just when calling this last function, saying that "fatal error: unexpectedly found nil while unwrapping an Optional value". I tried to use the debugger, and I found out that Firebase reference to Storage variable says "variable not available".
Could someone of you guys help me with this? I think I read the Firebase doc about a hundred times, and still can't get the point.
Thank you!
Downloading an image from a remote server is an asynchronous task, that means that the result is not immediately available. This is the reason that gsReference.downloadURL accepts a completion callback as an argument, and has no return value.
Since your function (downloadImageProductFromFirebase) is simply a wrapper to gsReference.downloadURL, it should also accept a completion callback as an argument, and should not have a return value (i.e. remove the -> UIImage?).
When you call self.downloadImageProductFromFirebase pass in a closure that receives the image, finds the index of the corresponding product in productList, and sets itself as the cell's image (assuming you're showing the image in the cell).
See this answer for how to asynchronously set cell images.
I'm trying to load some pictures from different URL in order to add them to my Table View Cells. Each cell contains an UIImageView and the pictures (UIImage) are not loading correctly.
Sometimes the URL connection fails, and sometimes not. I'm going crazy,
help me please!!
This is the part of my code that tries to download a picture from an specific URL:
let imageURL = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Angels_Stadium.JPG/1920px-Angels_Stadium.JPG")!
let task = URLSession.shared.dataTask(with: imageURL as URL) { (data, response, error) in
guard error == nil, let data = data else { return }
let downloadedImage = UIImage(data: data)
self.foto = downloadedImage!
tableWiew.reloadData()
}
task.resume()
This code is inside the init function of my class (User). After instantiate the class, I try to add the picture to my tableview in other class like this:
let user:User = User(json: obj, tableWiew: self.tableView)
addCell(cell: cell, name: user.nombre, job: user.puesto, nIdeas: "0", mProp: "0", image: user.foto)
The "addCell" method just creates a custom cell and inserts it into my tableView. That's working fine except for the damn picture.
If I add a picture from my Assets it works fine, but I don't know what's wrong when I try to add a picture from an URL.
Please download an image inside cellForRowAt method of UITableViewDataSource and if possible use SDWebImage that will help you with many options eg placeholder. It handles the caching internally and it is async.
let imageURL = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Angels_Stadium.JPG/1920px-Angels_Stadium.JPG")!
let request = URLRequest(url: imageURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {data, response, error -> Void in
// print("Task completed")
// print(response)
DispatchQueue.main.async(execute: {
self.imgVW.image = UIImage(data: data!)
})
})
task.resume()
if let toID = message.chatPartnerId() {
firebaseReference.child(toID).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: Any] {
cell.nameLabel.text = dictionary["displayname"] as? String
let pic = dictionary["pictureURL"] as! String
print("THIS IS THE URL FOR EACH DISPLAYNAME")
print(dictionary["displayname"] as? String)
print(pic)
if let imageFromCache = MainPageVC.imageCache.object(forKey: pic as NSString) {
cell.pictureLabel.image = imageFromCache
} else {
let requested = URLRequest(url: URL(string: pic )!)
URLSession.shared.dataTask(with: requested) {data, response, err in
if err != nil {
print(err)
} else {
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
MainPageVC.imageCache.setObject(imageToCache!, forKey: pic as NSString)
//cell.pictureLabel.image = nil
cell.pictureLabel.image = imageToCache
}
}
}.resume()
}
}
})
}
return cell
}
I'm running this code in my cellForRowAtIndexPath and I'm getting a ton of really bad behavior. I'm also getting similar behavior on other pages but for some reason this block of code with about a 90% consistency returns incorrect information for cells.
I get a lot of duplicate pictures being used, displaynames in the wrong places, but when I'm actually clicking into a person, my detail page shows the correct information every single time. That code is the typical didSelectRowAtIndexPath and passing the person.
What I don't understand is why on the initial load of this page all of the information is screwed up, but if I click into someone and come back the entire tableview has correct names and pictures. The names/pics also fix if I scroll a cell off the screen then come back to it.
I'm getting this behavior all over my app, meanwhile I see caching/loading done like this everywhere. Is it because I'm running the code in my cellForRowAtIndexPath? The only difference I see is that I'm running it there instead of creating a function inside of my Person class that configures cells and running it like that. What I don't understand is why that would make a difference because as far as I'm aware running a function within cellforRowAtIndexpath would be the same as copy-pasting that same code into there?
Any ideas/suggestions?
Edit: I'm getting a very similar situation when I'm running the following code:
self.PersonalSearchesList = self.PersonalSearchesList.sorted{ $0.users > $1.users }
self.tableView.reloadData()
Where I'm sorting my array before reloading my data. The information sometimes loads in incorrectly at first, but once I scroll the cell off the screen then come back to it it always corrects itself.
if you are using swift 3 here are some handy functions that allow you to save an image to your apps directory from an URL and then access it from anywhere in the app:
func saveCurrentUserImage(toDirectory urlString:String?) {
if urlString != nil {
let imgURL: URL = URL(string: urlString!)!
let request: URLRequest = URLRequest(url: imgURL)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
(data, response, error) -> Void in
if (error == nil && data != nil) {
func display_image() {
let userImage = UIImage(data: data!)
if let userImageData = UIImagePNGRepresentation(userImage!) {
let filename = self.getDocumentsDirectory().appendingPathComponent("userImage")
try? userImageData.write(to: URL(fileURLWithPath: filename), options: [.atomic])
}
}
DispatchQueue.main.async(execute: display_image)
}
})
task.resume()
}
}
and then access it with any view controller using this:
extension UIViewController {
func getImage(withName name: String) -> UIImage {
let readPath = getDocumentsDirectory().appendingPathComponent(name)
let image = UIImage(contentsOfFile: readPath)
return image!
}
}
and finally calling it like this:
cell.pictureLabel.image = getImage(withName: "userImage")
If you can run the saveCurrentUserImage function prior to running cellForRowAtIndexPath then you can just check if the photo is nil in the directory before attempting to download it. You might be getting funny behavior when the page initially loads because you have multiple network calls going on at once. I wouldn't recommend making any network calls in cellForRowAtIndexPath because every time the cells are re-initialized it's going to make that network call for each cell.
Hope it helps!
EDIT: This method of image saving and retrieval is for images that you want to persist. If you want to erase them from memory you'll have to delete them from your directory.