PFObject 值可能没有类:UIImage [英] PFObject values may not have class: UIImage

查看:15
本文介绍了PFObject 值可能没有类:UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 PFGiveItem 的 PFObject 子类.

I have a subclass of PFObject called PFGiveItem.

@interface PFGiveItem : PFObject <PFSubclassing>

+(NSString *)parseClassName;
@property (strong, nonatomic) NSString *giveItemName;
@property (strong, nonatomic) UIImage *giveItemImage;

@end

当我尝试查询已保存到 Parse 的图像,然后将图像保存到我的每个 GiveItems 时,出现错误.这是包含它自己的 PFQuery 的更大循环的一部分;我只是隔离这部分以保持简单,因为它的其余部分有效.

When I try to query for images I have saved to Parse, and then save the images to each of my GiveItems, I get an error. This is part of a larger loop which includes its own PFQuery; I'm just isolating this part to keep it simple because the rest of it works.

PFQuery *queryForRelatedImages = [PFQuery queryWithClassName:@"giveItemPhoto"];
[queryForRelatedImages whereKey:@"objectId" equalTo:@"pAwHU2e7aw"];
[queryForRelatedImages findObjectsInBackgroundWithBlock:^(NSArray *photos, NSError *error) {
    PFFile *imageFile = photos[0][@"imageFile"];
    NSLog(@"%@", imageFile);
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error){
            newGiveItem.giveItemImage = [UIImage imageWithData:data];
        }
    }];
}];

当我运行此代码时出现错误

When I run this code I get the error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values may not have class: UIImage'

这似乎没有意义,因为 PFGiveItem 类的给定属性实际上是 UIImage 类型.

Which doesn't seem to make sense since the given property of the PFGiveItem class is in fact of the type UIImage.

推荐答案

你必须使用 PFFile,至少现在是这样.看来 PFObjectSubclassing,不能将 UIImage 动态处理为 PFFile.处理此问题的一种简单方法是使用 UIImage 公共属性,然后使用 PFFile 私有属性.

You must use PFFile, at least for now. It seems PFObjectSubclassing, can't dynamically process a UIImage to PFFile. A simple way of handling this is to use a UIImage public property, and then a PFFile private property.

#import "PFObject.h"

@interface PFGiveItem : PFObject <PFSubclassing>
 @property (retain) UIImage *image;
@end

.m

#import "PFGiveItem.h"
@interface PFGiveItem()
 @property (retain) PFFile *imageFile;
@end

@implementation PFGiveItem
 @dynamic imageFile;
 @synthesize image = _image;
-(UIImage *)image
{
    if (!_image){
        [self fetchIfNeeded];
        _image = [UIImage imageWithData:[self.imageFile getData]];
    }
    return _image;
}
-(void)setImage:(UIImage *)image
{
    _image = image;
    self.imageFile = [PFFile fileWithData:UIImagePNGRepresentation(image)];
}
@end

子类项的实现很简单,您只需像处理任何其他局部变量一样使用 UIImage.它有助于 fetchInbackground 然后使用图像来保持主线程的解析:

Implementation of the subclassed item is simple, you just work with the UIImage like any other local variable. It helps to fetchInbackground and THEN use the image to keep parse off the main thread:

PFGiveItem *giveItem = (PFGiveItem *)[PFObject objectWithoutDataWithClassName:@"TableName" objectId:@"objectId"];
[giveItem fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error && object) {
        self.backgroundImageView.image = giveItem.image;
    }
}];

这篇关于PFObject 值可能没有类:UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆