问题
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.
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.
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.
Thanks in advance!!
回答1:
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
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
回答2:
You can set image using parse by below code...If you are storing image as 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..
- (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..
回答3:
Loading an image from parse is easy with the PFImageView class. 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):
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...
回答4:
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
Hope it helps for someone else!!
And thanks to all who took the time to answer my question!
来源:https://stackoverflow.com/questions/21846077/how-to-load-an-image-from-parse-into-a-uiimageview-ios