cannot retrieve user profile picture Parse iOS swift - ios

Im having problem with this line
let userImageFile:PFFile = user["photo"] as PFFile
code doesnt go beyond from this line. i tried searching everywhere in internet. but couldn't understand the error. in my below code im always getting "Something error". but my username retrieve works fine.
var user = spread.objectForKey("spreader") as! PFUser
user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
if obj != nil {
var fetchedUser = obj as! PFUser
var username = fetchedUser["username"] as! String
cell.username.text = user.username
//Profile Picture Retrieve
if let userImageFile:PFFile = user["photo"] as? PFFile{
println("working")
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
if imageData != nil{
cell.profileImage.image = UIImage(data:imageData!)
}else{
println("No Data")
}
}else{
println(error)
}
}
}else{
println("Something Error") // Always getting this
}
}
}
return cell
}

Related

PFFile to UIImage [duplicate]

How do i convert an PFFile to an UIImage with swift?
In this code, the app is getting the file from parse and now i want it to show on a UIImageView, But i get an error...
Here is my code...
var ImageArray:UIImage = [UIImage]()
var textArray:String = [String]()
var query = PFQuery(className:"ClassName")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
//this works fine
self.textArray.append(object.valueForKey("Image")! as! String)
//this does not work...
self.ImageArray.append(object.valueForKey("Image")! as! PFFile)
//I get an error that says PFFile is not convertible to UIImage. How do i convert it?
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
PFFile is a parse representation of anykind of file. To get the "real" file (image), you need to call getDataInBackgroundWithBlock. Try it:
if let userPicture = object.valueForKey("Image")! as! PFFile {
userPicture.getDataInBackgroundWithBlock({
(imageData: NSData!, error NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.ImageArray.append(image)
}
})
}
Swift 1.2+ Version
if let userPicture = PFUser.currentUser()!.valueForKey("Image") as? PFFile {
userPicture.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data:imageData!)
self.ImageArray.append(image)
}else{
print("Error: \(error)")
}
}
}
With Swift 3 on Parse.
if let photo = obj.file as? PFFile {
photo.getDataInBackground(block: {
PFDataResultBlock in
if PFDataResultBlock.1 == nil {//PFDataResultBlock.1 is Error
if let image = UIImage(data:PFDataResultBlock.0!){
//PFDataResultBlock.0 is Data
photoCell.attachedPicture.image = image
}
}
})
}

GetDataInBackgroundWithBlock | imageData is returning a nil value

So I am saving some objects to the localDatastore using parse.
//Pin the objects here!!
var imageObject = PFObject(className: "img")
imageObject["theFile"] = imageFile
imageObject.pinInBackgroundWithBlock({ (success, error) -> Void in
if error == nil {
imageObject.saveEventually()
println("object pinned in background")
} else {
println(error)
}
})
Then in the viewDidLoad I am querying the objects and appending them into an array of PFFile's
var query = PFQuery(className: "img")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil{
var objects : AnyObject = objects as! AnyObject
for object in objects as! [AnyObject]{
self.testImages.append(object["theFile"] as! PFFile)
}
} else {
println(error)
}
}
With this array now containing data. I am trying to insert the images into the tableView
if testImages.count >= 1{
testImages[indexPath.row].getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil{
//The app crashes here ~ fatal error: unexpectedly found nil while unwrapping an Optional value
cell.theImage.image = UIImage(data: imageData!)
} else {
println(error)
}
})
}
Because you are using a tableview, I think the best way is to download all the images from Parse and save it to an array. Then in the cellForRowAtPath method then you assign each index of that array to the cell.imageView.image
var query = PFQuery(className:"img")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objects = objects as? [PFObject]
{
for one in objects
{
var pictureImage = one["theFile"] as! PFFile
pictureImage.getDataInBackgroundWithBlock({ (dataToget:NSData?, error:NSError?) -> Void in
if error == nil
{
if let Image = UIImage(data: dataToget)
{
// save the image to array
// reload the tableview
}
}
})
}
}
}
}

Retrieve profile picture of each user parse iOS swift

In my iOS app. there is twitter like feed. and each feed has username, profile picture and the tweets he has added.
and im able to retrieve username to each feed but cannot retrieve the profile image. here is my code
let spread:PFObject = self.LiveFeedData.objectAtIndex(indexPath.row) as! PFObject
var user = spread.objectForKey("spreader") as! PFUser
user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
if obj != nil {
var fetchedUser = obj as! PFUser
var username = fetchedUser["username"] as! String
cell.username.text = user.username // This Works FINE
}
}
if let userImageFile = user["photo"] as? PFFile{
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if (error != nil) {
if imageData != nil{
cell.profileImage.image = UIImage(data:imageData!)
}else{
println("No Data")
}
}else{
println(error)
}
}
}
As discussed, using a PFImageView may help streamline the code that you need to move into your fetch closure.
user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
if obj != nil {
var fetchedUser = obj as! PFUser
var username = fetchedUser["username"] as! String
cell.username.text = username
cell.profileImage.file = fetchedUser["photo"] as! PFFile
cell.profileImage.fetchInBackground()
}
}

Problems When Trying to display images from Parse using Swift

I am trying to display an image from parse using swift. This is my code:
var query = PFQuery(className: "Maps")
query.getObjectInBackgroundWithId("1234asdf3456") {
(object: PFObject?, error: NSError?) -> Void in
if error == nil
{
println(object)
var objectAsPF = object as PFObject!
let file = objectAsPF["imageFile"] as! PFFile
file.getDataInBackgroundWithBlock {
(imageData:NSData?, error:NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let map:UIImage = UIImage(data: imageData)!
self.MapView.image = map
println("success")
}
}
}
}
else
{
println(error)
}
}
I set a breakpoint at println("success") and i checked the variable values and everything is fine until i try to convert imageData to UIImage. Any tips?
Use this code to retrieve images from parse then convert it from a PFFile to a UIImage...
var query = PFQuery(className:"Maps")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.scored = objects!.count
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
let userImageFile = object["imageFile"] as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
if image != nil {
self.imageArray.append(image!)
}
}
}
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
dispatch_async(dispatch_get_main_queue()) {
println("Finished Loading Image")
}

Something about all UI work must be done on the main thread in swift?

I am learning to use Swift to communicate with Parse. And I found some doubts here.
The following is two segmentations of codes in which I use Parse to find associated user and its avatar. The first one works well but I figured I supposed to put all UI work on the main thread, so I change it to the second one. Now, I'm wondering if there's something incorrect about doing it this way?
findSweeter.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]!, error:NSError!)->Void in
if error == nil {
let user:PFUser = (objects as NSArray).lastObject as PFUser
cell.usernameLabel.text = user.username
let profileImage:PFFile = user["profileImage"] as PFFile
profileImage.getDataInBackgroundWithBlock {
(imageData:NSData!, error:NSError!)->Void in
if error == nil {
let image:UIImage = UIImage(data: imageData)
cell.profileImageView.image = image
}
}
}
}
findSweeter.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
let user:PFUser = (objects as NSArray).lastObject as PFUser
let profileImage:PFFile = user["profileImage"]? as PFFile
profileImage.getDataInBackgroundWithBlock { (imageData:NSData!, error:NSError!)->Void in
if error == nil {
let image:UIImage = UIImage(data: imageData)!
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.usernameLabel.text = user.username
cell.profileImageView.image = image
})
}
}
}
}

Resources