我有一个名为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类型。
答案 0 :(得分:3)
至少目前你必须使用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;
}
}];