querying data and images into a tableview from parse.com using swift - ios

I'm looking to query data and images into a tableview using swift but, I can't seem to find any tutorials or anything showing how to do it. In my parse class I have a class named Products and in that class I have a column called productName, productImage, and productDescription. Than in my table view I have it where it shows the Image and the productName. Than it's suppose to when you click on the cell query over to a viewController to show the Image, the name and the description in a textView. If someone could please give me a hand that would be much appreciated.!
// Display product image
var initialThumbnail = UIImage(named: "question")
cell.ProductImage.image? = initialThumbnail!
if let thumbnail = object?["productImage"] as? PFFile {
cell.ProductImage.file = thumbnail
cell.ProductImage.loadInBackground()
}
It seems that there is something wrong with this code because when I run my app it shows just my question marked picture and not the image from parse.
Update:
I was able to have my pictures show up i didn't have the UIImage in the right class. However I still can't get the image to show up with the other data in the view controller after you click on the cell. only the description and name show up. Any ideas?
This is for the image i posted:vvvvvv
Pretty sure i have to add something here but not really sure what to get the image to query over when the product is selected from the table view
Update: Found out what the problem was. I added this code to it and it works now.
if let object = currentObject {
productName.text = object["productName"] as? String
productDescription.text = object["productDescription"] as! String var initialThumbnail = UIImage(named: "question")
ProductImage.image? = initialThumbnail!
if let thumbnail = object["productImage"] as? PFFile {
ProductImage.file = thumbnail
ProductImage.loadInBackground()

have a look at: https://parse.com/docs/ios/guide#user-interface-pfquerytableviewcontroller, this is Parses own user interface which produces a table view from a parse query. It also explains how you can add a thumbnail to the cell. Each cell will contain the object so you could easily segue to the image the cell represents. I hope this helps!

Related

Could someone help me with a weird firebase error while posting images?

Im trying to upload a post on firebase which contains: one thumbnail image, and unlimited subposts (like the content of the thumbnail). I have 3 steps on posting the pictures.
1: handle the upload image task.
2: create a "bulkUpload" function that creates image paths for each subpost.
3: call the "bulkUpload" (which also calls the upload image task)
The structure looks something like this:
(postid) {
author: (author)
likes: (number)
pathToImage: (path)
postId: (id)
subposts {
(id): (path)
(id): (path)
...etc
}
userId: (userid)
}
Simple and gets it working. But, not really. There is a strange problem that occurs with the subposts.
When I post the post, everything works, except for the subposts. The subposts, when posting the set of images for the first time, don't show.
Without adding or removing subposts, I tried posting for the second time, this time, the subposts do show. But double the amount I selected in the picker and what shows in the imageViews.
I will link the code from pastebin since its a little lengthy (and stackoverflow doesn't like a lot of code).
But hope I can get this working.
https://pastebin.com/rpLZT6nm
#Matt is right, in your case you should update your data model, which is presented by the collectionView and call reloadData or reloadItems(at:)
self.updateModel();
self.collectioView.reloadData()
self.collectioView.reloadItems(at: [IndexPath(item: 0, section: 0)]);
But if you need direct acces to visible cells, you can use other workflow:
let image : UIImage? = nil
let path = IndexPath(item: 0, section: 0)
let visiblePaths = self.collectioView.indexPathsForVisibleItems
if visiblePaths.contains(path) {
if let cell = self.collectioView.cellForItem(at: path) as? UploadSubPostCell {
cell.previewStep.image = image;
}
}
You can use such workflow to update only visible cells, because invisible cells will be reused before show to the screen

Asynchronous image loading from Core Data in Swift

I am building an application which uses a Core Data database to store its data on products. These products are displayed in a UICollectionView. Each cell in this collection view displays basic information on the product it contains, including an image.
Although the cells are relatively small, the original images they display are preferably quite large as they should also be able to be displayed in a larger image view. The images are loaded directly from Core Data in my CellForItemAtIndexPath: Method:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionWineCell
var current: Product!
//product details are set on labels
//image is set
if current.value(forKey: "image") != nil {
let image = current.value(forKey: "image") as! WineImage
let loadedImage = UIImage(data: image.image)
cell.imageview.image = loadedImage
} else {
cell.imageview.image = UIImage(named: "ProductPlaceholder.png")
}
return cell
}
When the collection of products grows, scrolling gets bumpier and a lot of frames are dropped. This makes sense to me, but so far I haven't found a suitable solution. When looking online a lot of documentation and frameworks are available for asynchronous image loading from a URL (either online or a file path), but doing this from Core Data does not seem very common.
I have already tried doing it using an asynchronous fetch request:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"ProductImage")
fetchRequest.predicate = NSPredicate(format: "product = %#", current)
let asyncRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { results in
if let results = results.finalResult {
let result = results[0] as! ProductImage
let loadedImage = UIImage(data: result.image)
DispatchQueue.main.async(execute: {
cell.wineImage.image = loadedImage
})
}
}
_ = try! managedObjectContext.executeRequest(asyncRequest)
However, this approach does not seems to smoothen things out either
QUESTION
When displaying large sets of data, including images, from Core Data, how does one load images in a way that it does not cause lags and frame drops in a UICollectionView?
If the images can be, as you say, quite large, a better approach is not to save them in Core Data but to put them in files. Store the filename in Core Data and use that to look up the file.
But that's not the immediate problem. Even with that you'll get slowdowns from spending time opening and decoding image data. A better approach is, basically, don't do that. In your collection views the images are probably displayed much smaller than their full size. Instead of using the full size image, generate a thumbnail at a more appropriate size and use that in the collection view. Do the thumbnail generation whenever you first get the image, whether from a download or from the user's photo library or wherever. Keep the thumbnail for use in the collection view. Only use the full size image when you really need it.
There are many examples online of how to scale images, so I won't include that here.

Swift 3 iOS 10 Reference to UIImageView to get Value for other function

I'm looking for a way to add codewise a way to reference to a UIImageView's value from another function.
I'm having a function which creates a new UIImageView for each instance in an array. Moreover, it also has an UITapGestureRecognizer added which calls a function when tapped. Now I want the function, which is called upon tap, to get the instance of that array which relates to the respective UIImageView.
I hope it's clear what I want to do. :)
Let's say you have an array of images:
let myImages = [UIImage(named: "image1"), UIImage(named: image2", UIImage(named: "image3"0]
And you have these images in three separate image views:
imageViewA.image = myImages[0]
imageViewB.image = myImages[1]
imageViewC.image = myImages[2]
Also, you've set up everything correctly that each image view calls the same function on a tap (we'll call it imageViewTapped()).
All you need to do is (1) properly set the tag property of each image view to point at the array index, (2) get the view that was tapped on, and (3) retrieve the image.
imageViewA.tag = 0
imageViewB.tag = 1
imageViewC.tag = 2
func imageViewTapped(_ recognizer:UITapGestureRecognizer) {
let tappedView = recognizer.view
let sourceImage = myImages[tappedView.tag]
}
I'd recommend setting everything up in a loop (tags, tap recognizer) and adding some type-casting in the function if needed. But I can't see your code to help you with that.

Adding two of the same values to array instead of one - swift

I am trying to make a search bar, where you can search for users names. The cell is showing two things - and image and a label.
The label is showing the users name and the image is showing the profile picture. The image needs the user userID to display his/her picture.
The countryAndCode contains the users userID called country, and the users name called code.
My problem is that it is adding the first user twice and I do not know why or how to change it.
Here is the relevant code, let me know if you would like to see more:
func startObersvingDB() {
FIRDatabase.database().reference().child("UserInformation").observeEventType(.ChildAdded, withBlock: { snapshot in
let title = snapshot.value!["usersUID"] as? String
let name = snapshot.value!["userName"] as? String
self.tableData.append(title!)
self.tableDataNames.append(name!)
for i in 0..<self.tableData.count {
print(self.tableDataNames.count)
print(self.tableData.count)
self.countryAndCode.append((self.tableData[i], code: self.tableDataNames[i]))//this shows error in XCode some times
//if the above code doesn't compile then use the below code
print("DONE")
print(self.countryAndCode)
}
self.tableView.reloadData()
})
}

action buttons on tweet cell swift

I am using twitter fabric and have a table view being populated with a 'tweetsWithJSONArray'.
I am trying to add the showAction boolean to my tweet cells in my tableview func cellForRowAtIndexPath method like so:
let tweet = tweets[indexPath.row]
let cell = feedTableView.dequeueReusableCellWithIdentifier(tweetTableReuseIdentifier, forIndexPath: indexPath) as! TWTRTweetTableViewCell
cell.configureWithTweet(tweet)
self.feedTableView.allowsSelection = true
cell.tweetView.delegate = self
cell.tweetView.showActionButtons = true
But I'm getting the error that TWTRTweetView dos not have a member named showActionButtons. I am a bit confused by this as the example on the fabric docs looks as follows:
let tweetView = TWTRTweetView(tweet: newlyLoadedTweet)
tweetView.showActionButtons = true
self.addSubview(tweetView)
link:
https://docs.fabric.io/ios/twitter/show-tweets.html
I realise I have it done a bit differently but I'm still unsure how to apply the showActionBoolean in the right way in my case. My tableview has an attached segment control and only when the final segment is selected do I populate my table with a tweetsWithJsonArray, that is why I couldn't take the example from the docs as is.
I had the same problem and after inspecting the SDK documentation I simply found out that setting "showTweetActions" to true works instead of
using the Fabric sample to do it (it gave me an error on "newlyLoadedTweet")
//swift
showTweetActions = true
Hope this can help ;)

Resources