如何将图像从Parse加载到UIImageView(iOS)

时间:2014-02-18 06:37:50

标签: ios objective-c uiimageview parse-platform

我可能会问一些非常简单的事情,但我找不到能够帮助我的教程或示例。

我已经学会了如何从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加载该信息的部分。

提前致谢!!

4 个答案:

答案 0 :(得分: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]]];

如果您不想下载图像格式网址,则必须将此PFFile转换为NSData,然后转换为UIImage

答案 1 :(得分: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];
        }
    }];

}

希望它对你有用..

答案 2 :(得分:1)

使用PFImageView类可以轻松地从解析中加载图像。下面是从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"];
}

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

答案 3 :(得分:1)

对于需要帮助的人!我找到了问题的答案。

在此示例中找到它: https://parse.com/questions/retrieve-images-from-parse-to-uiimageview

希望对其他人有帮助!!

感谢所有花时间回答我问题的人!