Auto Saving a Photo in Parse - ios

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()
}
}
}
}

Related

iOS How to fetch profile image for each user use Parse Swift

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
}
}
}

Error on displaying image asynchronously with Parse

I'm building app and I need to populate a TableView with some users infos, like username and the profile picture. The following code works for downloading and displaying the image.
if let imageFile = usersArr[indexPath.row]["profileImage"] as? PFFile {
let data = try? imageFile.getData()
cell?.imageView?.image = UIImage(data: data!)
}
But this is synchronously and I want it to be asynchronously. So I tried this.
if let imageFile = usersArr[indexPath.row]["profileImage"] as? PFFile {
imageFile.getDataInBackgroundWithBlock({ (data, error) -> Void in
if let imageData = data where error == nil {
cell?.imageView!.image = UIImage(data: imageData)
} else {
cell?.imageView!.image = UIImage(named: "defaultProfile")
}
})
}
But it's not working and I can't figure out why. Not even the defaultProfile image is appearing.
Any tips?
The issue is that the view is not being refreshed once the data has been fetched and image has been set.
I recommend using PFImageView from the ParseUI framework. It is a Parse-made image view which will automatically handle displaying a placeholder while loading, updating the data once available, and refreshing the view once the download is complete. Here's an example usage.
let imageView = PFImageView()
// Set placeholder image
imageView.image = UIImage(named: "placeholder")
// Set remote image
imageView.file = profilePicture
// Once the download completes, the remote image will be displayed
imageView.loadInBackground { (image: UIImage?, error: NSError?) -> Void in
if (error != nil) {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
} else {
println("image loaded")
}
}

UIImage not converting NSData properly

I am working on an app using Parse as backend. Whenever I try to load an image the NSData that I get is not empty but whenever I try converting it to an UIImage the image I get is nil.
I already checked all the images on Parse and I can see/get all of them from the database. I also re-uploaded the images just in case something was wrong with them.
Here is the piece of code I get the NSData and convert it to UIImage:
let myImage = object.objectForKey("imagem") as! PFFile
myImage.getDataInBackgroundWithBlock({(imageData : NSData?, error : NSError?) -> Void in
if error == nil {
if let imageData = imageData{
if let newImage = UIImage(data: imageData){
myClass.imagem = newImage
}
}
}
self.collectionView?.reloadData()
})
try this code. I hope it will help you.
let userImageFile = anotherPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock
{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil
{
if let imageData = imageData
{
let image = UIImage(data:imageData)
}
}
}
For more details , you can refer parse documentation. see this https://www.parse.com/docs/ios/guide#files-images
It turns out the way I was doing was correct, but I was working through a proxy and whenever the data was being sent to my app it was not returning the full NSData. So the problem lies within the proxy and not the code. Thanks for the replies.
Try using following code snippet
if let imageData = imageData
{
var newImage : UIImage! = UIImage(data: imageData!)
if image
{
myClass.imagem = newImage
}
}

How to pull file from user class parse swift

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.

Issues retrieving PFFile from Parse

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

Resources