Parse & Swift- Randomizing images - ios

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

Related

trying to retrieve image from parse.com but getting error

func loadImages() {
var query = PFQuery(className: "CollegeCover")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
let imageObjects = objects! as [PFObject]
for object in objects! {
let thumbNail = object["image"] as! PFFile
thumbNail.getDataInBackgroundWithBlock{ (objects, error) -> Void in
if (error == nil) {
let imageObjects = objects as? [NSData?]
let image = UIImage(data:objects!)
self.userImageView.image = image
print(image)
}}}
}
else{
print("Error in retrieving \(error)")
}
}//findObjectsInBackgroundWithblock - end
}
am trying to retrieve an image which is stored in parse.com's server but i don't know why am getting an error , and am not sure that my approach for getting the image is right or not so please help me if any body knows how to do it rightly or what am missing or doing wrong the error am getting is
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
I might be wrong, but I think that the only reason you get a nil result is because you are using an incorrect column name, i.e. "image". Are you sure this is the exact naming of your column in Parse?
Once you're past that error, you should be able to get the image to load fine since your code seems OK. You could however also consider PFImageView and save yourself the trouble of the extra coding since you can specify a PFFile object to the its file property. See here for an example.

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

How do I load an image from parse in swift?

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.

Resources