简体   繁体   English

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

[英]How to load an image from Parse into a UIImageView (iOS)

I might be asking something really easy, but I can't manage to find a tutorial or example that would help me. 我可能会问一些非常简单的事情,但我找不到能够帮助我的教程或示例。

I have learned how to retrieve string data from Parse and now I am trying to retrieve an image thinking it would be easier.. but I can't figure it out. 我已经学会了如何从Parse中检索字符串数据,现在我正在尝试检索图像,认为它会更容易......但我无法弄明白。

I am trying to load 1 image (that I'll be changing every day) in the UIImageView , retrieving the image from my data browser in Parse.com. 我试图在UIImageView加载1个图像(我将每天更换),从Parse.com中的数据浏览器中检索图像。

Could somebody help please? 有人可以帮忙吗?

Here is what I've done: 这就是我所做的:

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

I am missing the part where you indicate the UIImageView to load that information. 我错过了指示UIImageView加载该信息的部分。

Thanks in advance!! 提前致谢!!

You can set image in UIImageView with the help of this code.If you want to set image in imageview from with the help of url you can try this code.For this you have to download SDWebImages library 您可以在此代码的帮助下在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]]];

And if you don't want to download image form url then you have to convert this PFFile in to NSData and then convert in to UIImage 如果您不想下载图像格式网址,则必须将此PFFile转换为NSData,然后转换为UIImage

You can set image using parse by below code...If you are storing image as PFFile.... 您可以通过以下代码使用解析设置图像...如果您将图像存储为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;

    }];

}

If you want how to retrieve the details and save it in loadimagesarray use below code.. 如果您想要如何检索详细信息并将其保存在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];
        }
    }];

}

Hope it works for you.. 希望这对你有用..

Loading an image from parse is easy with the PFImageView class. 使用PFImageView类可以轻松地从解析中加载图像。 Below is an example of loading a PFFile from Parse, and showing it in a PFImageView, (or using a local placeholder image if remote file is not found): 下面是从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"];
}

In your case, you would probably get the image from the array you've just retrieved... 在您的情况下,您可能会从刚刚检索到的数组中获取图像...

For anyone who needs help! 对于任何需要帮助的人! I found the answer to my question. 我找到了问题的答案。

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

Hope it helps for someone else!! 希望对别人有帮助!

And thanks to all who took the time to answer my question! 感谢所有花时间回答我的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM