Retrieve profile picture of each user parse iOS swift - ios

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

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

cannot retrieve user profile picture Parse iOS swift

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
}

Fetching data from Parse.com into custom cell (swift)

I am attempting to fetch data from parse.com into my custom cell which is full of strings and images. I believe I am either retrieving my PFFile incorrectly from parse.com or I am retrieving the PFFile correctly but converting the file to UIImage improperly. The error i am receiving is going on within the loadData() function. It reads as follows: could not find an overload for 'init' that accepts the supplied arguments
Information
//Used to set custom cell
class Information {
var partyName = ""
var promoterName = ""
var partyCost = ""
var flyerImage: UIImage
var promoterImage: UIImage
init(partyName: String, promoterName: String, partyCost: String, flyerImage: UIImage, promoterImage: UIImage) {
self.partyName = partyName
self.promoterName = promoterName
self.partyCost = partyCost
self.flyerImage = flyerImage
self.promoterImage = promoterImage
}
}
Parse fetch function
func loadData() {
var findDataParse:PFQuery = PFQuery(className: "flyerDataFetch")
findDataParse.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
if (error == nil) {
for object in objects! {
var eventImage0 : UIImage
var eventImage10 : UIImage
let userImageFile = object["partyFlyerImage"] as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let eventImage = UIImage(data:imageData!)
eventImage0 = eventImage!
}
}
let userImageFile1 = object["partyPromoterImage"] as! PFFile
userImageFile1.getDataInBackgroundWithBlock {
(imageData1: NSData?, error1: NSError?) -> Void in
if error1 == nil {
let eventImage1 = UIImage(data:imageData1!)
eventImage10 = eventImage1!
}
}
//Error below
var party1 = Information(partyName: (object["partyName"] as? String)!, promoterName: (object["partyPromoterName"] as? String)!,partyCost: (object["partyCost"] as? String)!, flyerImage: UIImage(data: eventImage0)!, promoterImage: UIImage(data: eventImage10)!)
self.arrayOfParties.append(party1)
}
}
self.tableView.reloadData()
}
}
You are fetching data from Parse in background, but processing on main thread. Try this:
func loadData() {
var findDataParse:PFQuery = PFQuery(className: "flyerDataFetch")
findDataParse.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
if (error == nil) {
for object in objects! {
var eventImage0 : UIImage
var eventImage10 : UIImage
let userImageFile = object["partyFlyerImage"] as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let eventImage = UIImage(data:imageData!)
eventImage0 = eventImage!
let userImageFile1 = object["partyPromoterImage"] as! PFFile
userImageFile1.getDataInBackgroundWithBlock {
(imageData1: NSData?, error1: NSError?) -> Void in
if error1 == nil {
let eventImage1 = UIImage(data:imageData1!)
eventImage10 = eventImage1!
var party1 = Information(partyName: (object["partyName"] as? String)!, promoterName: (object["partyPromoterName"] as? String)!, partyCost: (object["partyCost"] as? String)!, flyerImage: UIImage(data: eventImage0)!, promoterImage: UIImage(data: eventImage10)!)
self.arrayOfParties.append(party1)
}
}
}
}
}
}
self.tableView.reloadData()
}
}
Your fetching your images Asynchronously and creating your Information cell Synchronously. So when you create the cell, the images are likely not loaded and your are in effect sending nil to the constructor for the cell.
In the following code you are retrieving the image data async:
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let eventImage = UIImage(data:imageData!)
eventImage0 = eventImage!
}
}
So when you assign the image data to eventImage0, the call to the Information cell initializer has probably already happened.
You need to modify the code to instead of passing the image into the cell view initializer, allow you to access the UIImageview from the Information cell, so that when the background image loads complete you can simply set the loaded image into that UI/PF/ImageView.

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