Andres Vlaeminck
Andres Vlaeminck

Reputation: 53

Parse load Images Swift 3.0

I'm runnig my own parse server and all works fine but I can't convert A PFFile to a UIImage, this is the error it throws at me:

Cannot convert value of type '(NSData?, NSError?) -> Void' to expected argument type PFDataResultBlock'

Here is the code I used:

var imageFromParse = object.object(forKey: "ProfilePicture") as! PFFile!
                        imageFromParse!.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                            var image: UIImage! = UIImage(data: imageData!)!
                        })

And all of this used to work perfectly in Swift 2.3. thank you for the help.

Upvotes: 1

Views: 1099

Answers (3)

egeelabs
egeelabs

Reputation: 151

Swift 3.0:

if let imageFromParse = user.object(forKey: "ProfilePicture") as? PFFile {
    imageFromParse.getDataInBackground(block: {
        (data: Data?, error: Error?) in

        if error == nil {

        }
    })

}

Main updates:

(data: NSData?, error: NSError?) in

has been updated to:

(data: Data?, error: Error?) in

And:

Swift 3 makes all labels required unless you specify otherwise, which means the method names no longer detail their parameters. In practice, this often means the last part of the method name gets moved to be the name of the first parameter. Find out more at: https://www.hackingwithswift.com/swift3

Upvotes: 3

Diogo Andre
Diogo Andre

Reputation: 658

Use Data and Error instead of NSData and NSError

Upvotes: 2

Ran Hassid
Ran Hassid

Reputation: 2788

i did some test on swift 3.0 and the following code works for me:

    let query = PFQuery(className: "FileTest")

    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        let firstObject = objects?.first as PFObject!
        let objectFile = firstObject.objectForKey("file") as! PFFile
        objectFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
            let image = UIImage(data: imageData!)
            if image != nil {
                self.imageOutlet.image = image
            }

        })
    }

In this code i first fetch all the FileTest collection, then i take the first object (just for test of course) and then i read the file. please notice that i an using objectForKey in order to get the PFFile which exist under the test column. After i have the file i call getDataInBackground to get the data, create UIImage from the data and update my imageView Everything works as expected.. try to run this code and see if it works for you.

Upvotes: 1

Related Questions