How to Convert an Pffile to an Uiimage with Swift

How to convert an PFFile to an UIImage with swift?

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

Converting PFFile to UIImage

I am doubtful that the class of the ImageView is UIImageView and not PFImageView...

Select your imageView and go to identity inspector, it must be appearing like this...

Must be this currently...

Should be this...

Should be like this...

As it is PFImageView that understands the getter and setter methods for property 'file'.

PFFile to UIImage with swift 3?

I Solved my problem

Swift 3 :

if let userPicture = object.valueForKey("Image")! as! PFFile {
userPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
let image = UIImage(data: imageData!)
if image != nil {
self.imageArray.append(image!)
}
})
}

PFFile as UIImageView

I ended up solving this issue. I made my custom cell a PFTableViewCell and turned the image into a PFImageView. I then used the following code, which works around converting the PFFile into a UIImage because PFImageView can accept a PFFile. This makes the downloading of the PFFile from Parse a lot smoother.

    cell.scorecardCellImage.file = scorecardData[indexPath.row].scorecardImage
cell.scorecardCellImage.loadInBackground()

CIImage Convert to UIImage

you will call init for resolve this issue

imgurl.image = UIImage.init(ciImage: transformedImage)


Related Topics



Leave a reply



Submit