I have the following method which primarily uses the username parameter to load the user's picture from the backend Parse.
func getProfilePicture(username: String) -> UIImage
{
var tempImage:UIImage = UIImage(named: "sample-qr-code.png")!
let query: PFQuery = PFQuery(className: "_User")
query.whereKey("appUsername", equalTo: username)
query.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in
for object in objects! {
let imageFiles = object["ProfilePic"] as! PFFile
imageFiles.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
tempImage = UIImage(data:imageData!)!
}
})
}
}
return tempImage
}
In the first line of the method, I am assigning a value to tempImage for the simple reason that it can't be empty otherwise it throws nil exception. When the code above runs, it goes through all the stages and conditions (detected by logging using print function) but the returned image is still sample-qr-code.png instead of the one in the database. There are no errors thrown from backend and this line gets executed:
tempImage = UIImage(data:imageData!)!
Can someone help me solve why this is not showing picture from backend? I am guessing it could be something to do with initialization and scope, but not certain.