【问题标题】:PFObject values may not have class: UIImagePFObject 值可能没有类:UIImage
【发布时间】:2014-07-25 23:50:09
【问题描述】:

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

@interface PFGiveItem : PFObject <PFSubclassing>

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

@end

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

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];
        }
    }];
}];

当我运行这段代码时,我得到了错误

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

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

【问题讨论】:

    标签: ios iphone cocoa-touch uiimage parse-platform


    【解决方案1】:

    至少现在你必须使用 PFFile。似乎 PFObjectSubclassing,无法将 UIImage 动态处理为 PFFile。处理此问题的一种简单方法是使用 UIImage 公共属性,然后使用 PFFile 私有属性。

    #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 然后使用图像来保持解析主线程:

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

    【讨论】:

    • 你能稍微解释一下最后一部分吗?这是在做什么? [PFObject objectWithoutDataWithClassName:@"TableName" objectId:@"objectId"]
    • 当你说 PFObjectSubclassing 不能将 UIImage 动态处理为 PFFile 时,你能稍微解释一下这意味着什么吗?
    • [PFObject objectWithoutDataWithClassName:@"TableName" objectId:@"objectId"] 正在使用从 Parse 检索对象的方法,如果您知道它的类名和对象 ID。
    • Parse SDK 执行一些交换以使 iOS 对象与其数据库系统兼容。它能够自动将 NSString、NSNumber 等对象转换为系统可以使用的格式。 UIImage 不是自动支持的格式之一,您必须自己进行一些转换。请注意,我对 PFFile *imageFile 使用了“@dynamic”,而不是 UIImage *image。 @dynamic 意味着您希望 Parse“神奇地”对 setter/getter 做它需要做的事情,但这种“神奇”只适用于某些对象类型。
    猜你喜欢
    • 2015-03-26
    • 2017-11-16
    • 1970-01-01
    • 2016-03-11
    • 2021-06-17
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多