如何使用swift将PFFile转换为UIImage?
问题描述:
如何使用swift将PFFile转换为UIImage?
How do i convert an PFFile to an UIImage with swift?
在此代码中,应用程序从解析中获取文件,现在我希望它显示在一个UIImageView,但我收到一个错误......
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是任何文件的解析表示。要获得真实文件(图像),您需要调用 getDataInBackgroundWithBlock
。试试吧:
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)
}
})
}