我正在使用parse.com制作一个简单的应用程序,它可以在线检索数据并填充我的表格。我已成功填充表格单元格文本标签,但当我尝试检索图像时,我的PFTableViewCells中没有显示任何内容。
我的代码如下:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("reuse", forIndexPath: indexPath) as PFTableViewCell
var image : UIImage = UIImage(named: "first")!
cell.textLabel?.text = object.objectForKey("Name") as? String
var thumbnail:PFFile = object.objectForKey("Image") as PFFile
cell.imageView?.image = UIImage(named: "first")
cell.imageView?.file = thumbnail
cell.imageView?.loadInBackground()
return cell
}
我如何设置它有什么不对吗?
答案 0 :(得分:0)
在Parse PFFile store中将Image作为二进制数据,这样你就可以在NSData中获取它并将该NSData转换为Image。我正在向您展示我用来从目标C中的解析对象获取图像的代码,希望它对您有用。
-(void)myParseObject:(PFObject *)obj
{
//postImageFile is key in which PFFile is stored in parse
[[obj objectForKey:@"postImageFile"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
// image can now be set on a UIImageView
}
}];
}
答案 1 :(得分:0)
我也从现有的例子中遇到了麻烦;这是最终奏效的。从解析中获取对象时:
let thumbnail = object["toImage"] as PFFile?
然后,在cellForRowAtIndexPath:
中// set thumbnail image if available (must set to placeholder image first!)
cell!.imageView!.image = UIImage(named: "Placeholder")
// get image from parse.com
let thumbView = PFImageView()
if thumbnail != nil {
thumbView.file = thumbnail
thumbView.loadInBackground({
(image, error: NSError!) in
if error == nil {
cell!.imageView!.image = image
}
})
}