Loading images from parse.com - swift

This is my saving image to parse:
func uploadPost(){
var imageText = self.imageText.text
if (imageView.image == nil){
println("No image uploaded")
}
else{
var posts = PFObject(className: "Posts")
posts["imageText"] = imageText
posts["uploader"] = PFUser.currentUser()
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
//**Success saving, now save image.**//
// Create an image data
var imageData = UIImagePNGRepresentation(self.imageView.image)
// Create a parse file to store in cloud
var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
posts["imageFile"] = parseImageFile
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
// Take user home
println("Data uploaded")
}
else{
println(error)
}
})
}
else{
println(error)
}
})
}
}
How can I load the images from parse? This is how my Parse.com "Posts" data looks like:
Any suggestions?
I think maybe something like using this:
self.imageView.sd_setImageWithURL(url, completed: block)
But I donĀ“t know how I get the URL. And what if the images has different names?

try something like this
let imageFile = pfObject["imageFile"] as? PFFile
if imageFile != nil{
imageFile!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.imageView.image = UIImage(data: imageData)!
}
}
}
}
Where pfObject is the reference to your object. There are other ways you could check for a nil value, but this should work.
As long as you have a reference to the correct object (which you can get via a query for objectId) then you should only need the name of the column the image is file is stored in and not the image file itself.

Related

Swift 3: cant get image from Parse?

Ok, I have looked at questions like How do you access an object's fields from a Parse query result in Swift? but the answer is not working in Swift 3. With the following trying to get an image from the first PFObject in Parse I get the error:
Cannot convert value type NSData, NSError -> Void to expected
PFDataResultBlock ?
var query = PFQuery(className: PARSE_CLASS!)
query.order(byDescending: "createdAt")
query.findObjectsInBackground {
(objects, error) -> Void in
if error == nil {
//print(objects?.first?["testTxt"] as! NSString)
//print(objects?.first?["testImg"] as! PFFile)
let thumbnail = objects?.first?["testImg"] as! PFFile
thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let image = UIImage(data: imageData!) {
//get image here
}
}
}
}
I have tried changing the type and everything. How can I store an image from Parse in recent Swift?
You can try this:
if let validObjects = objects {
for object in validObjects {
let thumbnail = object["testImg"] as? PFFile
thumbnail?.getDataInBackground (block: { (data, error) -> Void in
//read image here
}
}
}

Get data from User table without current user

I'm trying to get all the user's from the _User table and get their images and put them into an array. The below code works when I am logged in (current user != nil)
but when I'm not logged in (current user = nil) I get no images at all
How can i achive this query without being a current user ???
I have the following function
let chefUserQuery = PFQuery(className: "_User")
chefUserQuery .whereKey("chef", equalTo: true)
chefUserQuery .findObjectsInBackgroundWithBlock{
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFUser] {
var ai = 0
for object in objects {
print("kati")
let imageChef = object["avatar"] as! PFFile
imageChef.getDataInBackgroundWithBlock({ (data, error) -> Void in
if error == nil {
if let imageData = data {
let imagatzaki = UIImage(data: imageData)
let idChef = object.objectId
self.imagesOfChef[ai] = chefImages(objidChef: idChef, imageChef: imagatzaki)
ai += 1
print(self.imagesOfChef)
} } })
}
completion(.Success())
}
}
else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
Thanks

In Swift, how do you check if pointer in Parse column is empty or not

Within my user object I added a column to add a users favorite team. The column is identified as favTeam and is a pointer to a teams class
Here is my code. I have populated my user with a favorite team however the logic is always showing that "favteam nil"
if let object = PFUser.currentUser()!["favTeam"] as? [PFObject]{
print("favteam not nil")
print(object)
let favTeam = PFUser.currentUser()!["favTeam"]
favTeamText.text = favTeam["Name"] as? String
if let favTeamImageView = favTeam["teamLogo"] as? PFFile {
favTeamImageView.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.teamLogo.image = UIImage(data: imageData)
}
}
}
}
}
else {
print("favteam nil")
}
I can accomplish this by using a PFUser.query() as follows...
func fetchFavoriteTeam() {
let userQuery: PFQuery = PFUser.query()!
userQuery.whereKey("username", equalTo: (currentUser?.username)!)
userQuery.findObjectsInBackgroundWithBlock({
(users, error) -> Void in
var favTeam = users!
if error == nil {
if favTeam != nil {
favTeamContainer = favTeam.valueForKey("favTeam") as! PFObject
}
} else {
print(error)
}
})
}

Get data from Parse.com to swift

In my code it receives the images from parse, and show it in a imageView. Here is the code:
http://pastebin.com/kDjAgPRT
If needed, here is my code for upload:
func uploadPost(){
var imageText = self.imageText.text
if (imageView.image == nil){
println("No image uploaded")
}
else{
var posts = PFObject(className: "Posts")
posts["imageText"] = imageText
posts["uploader"] = PFUser.currentUser()
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
//**Success saving, now save image.**//
// Create an image data
var imageData = UIImagePNGRepresentation(self.imageView.image)
// Create a parse file to store in cloud
var parseImageFile = PFFile(name: "upload_image2.png", data: imageData)
//var parseImageFile = PFFile(data: imageData)
posts["imageFile"] = parseImageFile
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
// Take user home
println(success)
println("Data uploaded")
}
else{
println(error)
}
})
}
else{
println(error)
}
})
}
}
As you can see, here is my Parse inside "Posts":
How can i also get "imageText", "uploader" and "createdAt" for the images? Like instagram has.
Try this:
struct Details {
var username:String!
var text:String!
var CreatedAt:NSDate!
var image:UIImage!
init(username:String,text:String,CreatedAt:NSDate,image:UIImage){
self.username = username
self.text = text
self.CreatedAt = CreatedAt
self.image = image
}
}
func QueryImagesFromParse(){
var arrayOfDetails = [Details]()
var query = PFQuery(className: "Posts")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let newObjects = objects as? [PFObject] {
for oneobject in newObjects {
var text = oneobject["imageText"] as! String
var username = oneobject["uploader"] as! String
var time = oneobject.createdAt
var userImageFile = oneobject["imageFile"] as! PFFile
userImageFile.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
if error == nil {
let newImage = UIImage(data: imageData!)
var OneBigObject = Details(username: username, text: text, CreatedAt: time!, image: newImage!)
arrayOfDetails.append(OneBigObject)
// then reloadData
}
})
}
}
}
}
}
SO NOW with the arrayOfDetails you could populate your cells...

Load image from parse.com

How do I load images and image text saved in parse.com?
This is my function for uploading the image:
func uploadPost(){
var imageText = self.imageText.text
if (imageView.image == nil){
println("No image uploaded")
}
else{
var posts = PFObject(className: "Posts")
posts["imageText"] = imageText
posts["uploader"] = PFUser.currentUser()
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
//**Success saving, now save image.**//
// Create an image data
var imageData = UIImagePNGRepresentation(self.imageView.image)
// Create a parse file to store in cloud
var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
posts["imageFile"] = parseImageFile
posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if error == nil{
// Take user home
println("Data uploaded")
}
else{
println(error)
}
})
}
else{
println(error)
}
})
}
}
How can i add image and text in tableview, and make it a scroll look-a-like like instagram if there are more then 1 image?
Here is the code to load an image from parse
PFFile *profile = user[#"profilePic"];
if (profile)
{
[profile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error)
{
UIImage *profilePic = [UIImage imageWithData:imageData];
//Here is your image use it
}
else
{
//error
}
}];
}
First part of question is duplicate as remus pointed out:
Retrieving image from parse.com You will need to query for the image data and convert to a uiimage.
Second part of question:
Just store the queried images and text in array, then make custom uitableviewcells that hold a uiimageview and textview. Populate the tableviewcells with data from array.