In my app, when a user signs up, he/she signs up, an image is added to the user class. The code used to do this is...
var newUser = PFUser()
let imageData = UIImagePNGRepresentation(self.imageView.image)
let imageFile = PFFile(data: imageData)
newUser.setObject(imageFile, forKey: "image")
newUser.signUpInBackgroundWithBlock({
(success, error) -> Void in
})
Later in my app, I want to pull that picture to put it into a UIImageView. The way I tried to do it was this.
var user = PFUser.currentUser() //Error on this line
let profileImage = user["image"] as! PFFile
However, this returns the error "AnyObject? is not convertible to PFFile". I would like to know how I can retrieve the file with the key "image" from the user class. Thanks for your help.
An image is stored in parse as a datafile. To retrieve the image again from parse, and load it to you UIImage. You need to convert the image
var user = PFUser.currentUser()
let userImageFile = user["image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
}
}
And change the let image to the image you want to load your image.
Related
I have an app in which I have news, which users are added and I want in a table view to show user photo which are taken from DB "_User" and I fetch data from class "news". My code is:
var user = PFUser.current()
let useravatar = user!["profilePicture"] as? PFFile
useravatar?.getDataInBackground{ (imageData, error)in
if imageData != nil {
let image = UIImage(data: imageData!)
cell.userPhoto.image = image
}
}
But this code loads only the current user photo, but I need the user photo for each row, how I can do this? Example:
As you see in pictures I have two user, but it loads only my profile photo.
Load images asynchronously
var user = PFUser.current() //you'e setting an image for same user
let useravatar = user!["profilePicture"] as? PFFile
useravatar?.getDataInBackground{ (imageData, error)in
DispatchQueue.main.async {
if imageData != nil, error == nil {
let image = UIImage(data: imageData!)
cell.userPhoto.image = image
}
}
}
I'm building an app that requires the user to have a photo. What I'm trying to do is autosave the placeholder photo until they choose the camera/photo gallery and choose a pick. My problem is that it's not happening. I've used the code from the Parse documentation as well as from my own choose photo source code that works. It still will not automatically save the photo when no photo is detected. I know finding nil and/or data in Parse is complicated. The problem may also be how I'm establishing my photo.image in the first place. If you have ideas on how to get my photo to save when a user doesn't have one please help. Here is my code.....
if let userPicture = PFUser.currentUser()?["userPhoto"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if !(error != nil)
{
if imageData != nil
{
self.profileIMG.image = UIImage(data: imageData!)
}
else
{
let image = UIImage(named: "blueplaceholder2.png")
let imageData = UIImageJPEGRepresentation(image!, 0.25)!
let imageFile = PFFile(name:"image.png", data:imageData)
let user = PFUser.currentUser()
user!.setObject(imageFile, forKey: "userPhoto")
user!.saveInBackground()
}
}
}
}
First, to be simple, how do I change a blank UIImage view to an image I have stored in Parse? This is my code so far
var query = PFQuery(className:"Content")
query.getObjectInBackgroundWithId("mlwVJLH7pa") {
(post: PFObject?, error: NSError?) -> Void in
if error == nil && post != nil {
//content.image = UIImage
} else {
println(error)
}
}
On top of just replacing the blank UIImageView, how may I make the image that it is replaced with random? I assume I can't use an objectId anymore, because that is specific to the row that it represents.
I would first retreive the objectIds from parse with getObjectsInBackgroundWithBlock, and then select a random objectId from that array in a variable called objectId. That way you save the user from querying every object from parse and using a lot of data doing it.
Second I would
getObjectInBackgroundWithId(objectId)
if error == nil {
if let image: PFFile = objectRow["image"] as? PFFile{
image.getDataInBackgroundWithBlock {
(imageData: NSObject?, error: NSError?) Void in ->
if let imageData = imageData {
let imageView = UIImageView(image: UIImage(data: imageData))
}
}
}
At least this works for me.
First off, you'll want to use query.findObjectsInBackgroundWithBlock instead of using query.getObjectInBackgroundWithId.
This will get you all of your PFObjects. Grab a random PFObject from the array it returns and now you have a single random PFObject that you can set the content.image to.
Let's say your PFObject has an 'image' attribute as a PFFile (what you should be using to store images with Parse.)
Simply convert the PFFile into a UIImage by doing something similar to the following:
if let anImage = yourRandomPFObject["image"] as? PFFile {
anImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
let image = UIImage(data:imageData)
content.image = image
}
}
I am creating an app in parse in which the user has an option to choose a profile picture when they sign up. This is the code for that.
var profilePictures = PFObject(className: "ProfilePictures")
let imageData = UIImagePNGRepresentation(self.profileImage.image)
let imageFile = PFFile(name:"image.png", data:imageData)
profilePictures["profilePhoto"] = imageFile
profilePictures["user"] = usernameField.text
profilePictures.save()
Later I have a screen in which a UIImageView needs to be populated with the chosen profile picture.
This works until the application itself is stopped completely and restarted.
The PFFile is then found as nil and I get the error "unexpectedly found nil while unwrapping an optional value".
Here is the code for displaying the picture.
override func viewDidAppear(animated: Bool) {
var query = PFQuery(className: "ProfilePictures")
query.whereKey("user", equalTo: PFUser.currentUser()?.username)
query.findObjectsInBackgroundWithBlock({
(success, error) -> Void in
let userImageFile = profilePictures["profilePhoto"] as! PFFile
//error is on the above line
userImageFile.getDataInBackgroundWithBlock({
(imageData: NSData?, error) -> Void in
var image = UIImage(data: imageData!)
self.profileImage.image = image
})
})
}
For some reason you are not getting userImageFile correctly set. It appears to be a nil. I would check the Parse console to confirm that you have an image in the PFile. In any case it may be smarter to use 'if let' to avoid the unwrapping problem. This will not solve the problem if there if PFile is not saved since as pointed below you should use saveInBackground and use notifications to confirm that you are ready for a retrieval.
if let userImageFile = profilePictures["profilePhoto"] as! PFFile {
//error is on the above line
userImageFile.getDataInBackgroundWithBlock({
(imageData: NSData?, error) -> Void in
var image = UIImage(data: imageData!)
self.profileImage.image = image
})
}
Your error is probably on saving:
let imageFile = PFFile(name:"image.png", data:imageData)
profilePictures["profilePhoto"] = imageFile
profilePictures.save()
You are saving an object with a pointer to a new unsaved PFFile, which leads to error. You should first do imageFile.saveInBackground, and use callback to assign imageFile on profilePictures, then save profilePictures.
You can confirme that by seeing on Parse's datastore that there is no value for key 'profilePhoto' on your profilePictures object
I was wondering if anyone can help me, I'm new to app developing
I am uploading images from my app to parse with no problem with help from parse documentation.
let imageData = UIImagePNGRepresentation(scaledImage)
let imageFile: PFFile = PFFile(data: imageData)
var userPhoto = PFObject(className: "postString")
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()
but when i add the code to retrieve the image back, I'm a bit lost :/
let userImageFile = anotherPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
}
}
where is the "anotherPhoto" coming from ? Parse did say "Here we retrieve the image file off another UserPhoto named anotherPhoto:"
anotherPhoto would be an instance of your postString (userPhoto in your upload example). The file downloading example that would work for you is like this:
let userImageFile = userPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
let image = UIImage(data:imageData)
}
}
Any PFFile must be referenced from a normal PFObject or it cannot be retrieved. PFFile objects themselves will not show up in the data browser.