ios - PFObject 值可能没有类 : UIImage

标签 ios iphone cocoa-touch uiimage parse-platform

我有一个名为 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/

相关文章:

ios - 核心数据 : no index path for table cell being reused, 单元格消失

iphone - 在 Nib 或代码中设计 iPhone 界面?

android - iphone 类似 android 的位置标记

iphone - 如何在 iPhone 上将声音名称传递给 Fmod

ios - 将 ECSlidingViewController 与 TableView 一起使用

html - 全宽图像在方向改变时扭曲

ios - Xamarin Test Cloud 是否支持 iOS 设备上的 React Native 应用程序?

ios - 如何为 UITableView 中的选定单元格制作透明背景

cocoa-touch - 具有 UITableViewCellStyleValue2 样式的单元格上 detailTextLabel 的宽度

ios - 浏览文本,解析链接