PFObject值可能不具有类:UIImage [英] PFObject values may not have class: UIImage

查看:119
本文介绍了PFObject值可能不具有类:UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  @interface PFGiveItem:PFObject< PFSubclassing> 

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

@end

当我尝试查询我已保存到解析,然后将图像保存到我的每个GiveItems,我得到一个错误。这是一个更大的循环的一部分,包括它自己的PFQuery;我只是隔离这部分,保持简单,因为其余的工作。

  PFQuery * queryForRelatedImages = [PFQuery queryWithClassName:@giveItemPhoto]; 
[queryForRelatedImages whereKey:@objectIdequalTo:@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];
}
}];
}];

当我运行此代码时,我收到错误

  ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:'PFObject值可能没有类:UIImage'
pre>

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

解决方案

您必须使用PFFile,至少现在。看来PFObjectSubclassing,不能动态处理一个UIImage到PFFile。一个简单的处理方法是使用UIImage的public属性,然后使用PFFile私有属性。

  #importPFObject.h

@interface PFGiveItem:PFObject< PFSubclassing>
@property(retain)UIImage * image;
@end

.m

  #importPFGiveItem.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和THEN使用图像保持解析主线程:

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


I have a subclass of PFObject called PFGiveItem.

@interface PFGiveItem : PFObject <PFSubclassing>

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

@end

When I try to query for images I have saved to Parse, and then save the images to each of my GiveItems, I get an error. This is part of a larger loop which includes its own PFQuery; I'm just isolating this part to keep it simple because the rest of it works.

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

When I run this code I get the error

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

Which doesn't seem to make sense since the given property of the PFGiveItem class is in fact of the type UIImage.

解决方案

You must use PFFile, at least for now. It seems PFObjectSubclassing, can't dynamically process a UIImage to PFFile. A simple way of handling this is to use a UIImage public property, and then a PFFile private property.

#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

Implementation of the subclassed item is simple, you just work with the UIImage like any other local variable. It helps to fetchInbackground and THEN use the image to keep parse off the main thread:

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

这篇关于PFObject值可能不具有类:UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆