如何将Parse中的图片加载到UIImageView(iOS)

4

我可能会问一些很容易的问题,但我找不到一个能帮助我的教程或示例。

我已经学会了如何从Parse中检索字符串数据,现在我正在尝试检索图像,认为这将更容易...但我无法弄清楚。

我正在尝试在UIImageView中加载1个图像(我每天都会更改),从Parse.com中检索图像。

有人可以帮忙吗?

这是我做过的:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self performSelector:@selector(retrieveFromParse)];
}

- (void) retrieveFromParse {

    PFQuery *retrieveImage = [PFQuery queryWithClassName:@"outfitDay"];
    [retrieveImage findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            loadimageArray = [[NSArray alloc] initWithArray:objects];
        }
    }];         
}

我没有看到您指示UIImageView加载该信息的部分。

提前感谢!


你是在 Parse 上存储图像(文件)还是图像的 URL? - Rajan Balana
loadimageArray 中的每个对象都是 PFFile 吗?(在 Parse 中,图像存储为 PFFile)。PFFile 类有一个名为 getDataInBackgroundWithBlock: 的方法,如果成功,则会返回可以转换为 UIImageNSData - Ivan Lisovyi
抱歉,是的,它们是PFFile。 - adriennemhenry
4个回答

8

您可以使用此代码设置UIImageView中的图像。如果您想要从url中设置imageview中的图像,则可以尝试此代码。为此,您必须下载SDWebImages库。

 PFObject *objFollow1 = ["your array of PFObject" objectAtIndex:your index];
 PFFile *image = [objFollow1 objectForKey:@"your key"];
 NSLog(@"%@",teaserImage.url);
 [your imageview setImageWithURL:[NSURL URLWithString:[teaserImage url]]];

如果您不想从URL下载图像,则必须将此PFFile转换为NSData,然后再将其转换为UIImage。


4
那个性感的 url 方法正是我所追求的。 - Khanh Nguyen

5
你可以使用以下代码通过解析设置图像...如果你将图像存储为PFFile....
PFFile *eventImage = [[loadimagesarray objectAtIndex:indexPath.row] objectForKey:@"ProfileImageFile"]; //ProfileImageFile is the name of key you stored the image file

if(eventImage != NULL)
{

    [eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

        cell.yourimageview.image = thumbnailImageView.image;

    }];

}

如果您想获取详细信息并将其保存在loadimagesarray中,请使用以下代码:
- (void)retrieveDetails
{
    PFQuery *query = [PFQuery queryWithClassName:@"outfitDay"];
    __block int totalNumberOfEntries = 0;
    [query orderByDescending:@"createdAt"];
    [query countObjectsInBackgroundWithBlock:^(int number1, NSError *error) {
        if (!error) {
            // The count request succeeded. Log the count

            totalNumberOfEntries = number1;

            if (totalNumberOfEntries > [loadimagesarray count])
            {
                NSLog(@"Retrieving data");
                //query.skip=[NSNumber numberWithInt:2300];
                [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    if (!error) {
                        // The find succeeded.
                        NSLog(@"Successfully retrieved %d chats.", objects.count);
                        int j=[loadimagesarray count];
                        if([objects count]>j)
                        {
                            [loadimagesarray addObjectsFromArray:objects];

                        }
                    }
                    else
                    {
                        // Log details of the failure
                        NSLog(@"Error: %@ %@", error, [error userInfo]);
                    }
                }];
            }

        } else
        {
            // The request failed, we'll keep the chatData count?
            number1 = [loadimagesarray count];
        }
    }];

}

希望这对你有用。

1
使用PFImageView类从Parse加载图像非常容易。以下是从Parse加载PFFile并在PFImageView中显示它的示例(如果找不到远程文件,则使用本地占位符图像):
PFImageView *profileImageView;

// User thumbnail
PFFile *imageFile = [self.author objectForKey:FIELDNAME_PROFILE_PROFILEPICTUREFILE];
if (imageFile) {
    profileImageView.file = imageFile;
    [profileImageView loadInBackground];
} else {
    profileImageView.image = [UIImage imageNamed:@"AvatarPlaceholder.png"];
}

在您的情况下,您可能会从刚检索到的数组中获取图像...

谢谢!对于下一个问题很抱歉...这段代码应该输入在viewDidLoad部分吗?就像我说的,我非常新手 :) - adriennemhenry
当然,这取决于您的使用情况。但是viewDidLoad可能非常可行。 - Marius Waldal

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接