gpt4 book ai didi

ios - PFObject 值可能没有类 : UIImage

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:44 24 4
gpt4 key购买 nike

我有一个名为 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 公共(public)属性,然后使用 PFFile 私有(private)属性。

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

关于ios - PFObject 值可能没有类 : UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24052510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com