2

嗨,我是使用 parse.com 的新手实际上我正在尝试将图像保存在 parse.com 中并重新检索它。但我成功地将图像保存为PFFile 在此处输入图像描述

在 parse.com 仪表板中显示如下

在此处输入图像描述

现在我如何检索图像并在 UIImageView 中显示

我试过这样但它不起作用..

 PFQuery *query = [PFQuery queryWithClassName:@"UserPhoto"];
    [query whereKey:@"userName" containsString:@"ravi"];
    [query getObjectInBackgroundWithId:@"ID" block:^(PFObject *object, NSError *error) {
        if(!error){
            PFFile *im = [object objectForKey:@"imageFile"];
            _getImg.image = im;
        }}];

在这里,我想检索特定图像userName ravi

4

2 回答 2

5

要检索图像,您需要:

  1. 查询有PFObject问题的
  2. 用于-[PFFile getDataInBackgroundWithBlock:]检索PFFile的数据
  3. UIImage用结果创建一个NSData

    PFQuery *query = [PFQuery queryWithClassName:@"UserPhoto"];
    
    [query whereKey:@"userName" containsString:@"ravi"];
    
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (!object) {
            return NSLog(@"%@", error);
        }
    
        PFFile *imageFile = object[@"imageFile"];
    
        [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!data) {
                return NSLog(@"%@", error);
            }
    
            // Do something with the image
            self.imageView.image = [UIImage imageWithData:data];
        }];
    }];
    

顺便说一句,除非纯粹是为了实验,或者您正在主线程之外执行一些操作,否则我会避免保存PFObject同步使用-[PFObject save]和使用异步版本,-[PFObject saveInBackgroundWithBlock:].

于 2013-08-29T05:04:33.520 回答
1

You cannot use getObjectInBackgroundWithId: method in your case. Instead you can use getFirstObjectInBackgroundWithBlock: method to retrieve the first instance.

于 2013-08-28T12:05:57.000 回答