PFObject值可能没有class:UIImage

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

@interface PFGiveItem : PFObject <PFSubclassing> +(NSString *)parseClassName; @property (strong, nonatomic) NSString *giveItemName; @property (strong, nonatomic) UIImage *giveItemImage; @end 

当我尝试查询已保存到Parse的图像,然后将图像保存到我的每个GiveItem中时,出现错误。 这是包含自己的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类的给定属性实际上是UIImagetypes的。

至less现在你必须使用PFFile。 看来PFObjectSubclassing,不能dynamic处理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 

subclassed项的实现很简单,你只需像使用任何其他局部variables一样使用UIImage。 它有助于fetchInbackground,然后使用图像保持parsing主线程:

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