我有一个名为 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 类型。
您必须使用 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;
}
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)