Download image from parse.com

十年热恋 提交于 2019-12-12 02:27:08

问题


I wanna download a image file form parse.com and displaying it in a ImageView "myImageView". I create for that a class called "Image" and a column "profilImage" as a File Column. I tried it with that code, but it still doesn't work.

PFQuery *query = [PFQuery queryWithClassName:@"Image"];
[query getObjectInBackgroundWithId:@"Y7ahcw9ZNR" block:^(PFObject *imageObject, NSError *error) {

PFFile *theImage = [imageObject objectForKey:@"profilImage"];
NSData *imageData = [theImage getData];
UIImage *yourImage = [UIImage imageWithData:imageData];

NSLog(@"%@", yourImage);

[myImageView setImage:yourImage];

}];

What am I doing wrong?


回答1:


Something to try is using the getDataInBackgroundWithBlock PFFile function. getData is synchronous and could block the UI thread depending on how it is used.

[theImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    myImageView.image = [UIImage imageWithData:data];
}];



回答2:


Save and Retrieve profilePic from Parse

-Objective-c

save an image to Parse curreentUser

NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
[imageFile saveInBackground];

PFUser *user = [PFUser currentUser];
[user setObject:imageFile forKey:@"profilePic"];
[user saveInBackground];

Retrieve image from Parse curreentUser

PFUser *cUser = [PFUser currentUser];
PFFile *pictureFile = [cUser objectForKey:@"profilePic"];

        [pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error){
                [_profileImage setImage:[UIImage imageWithData:data]];
            }
            else {
               // there is no profile picture.

            }
        }];

-Swift

save an image to Parse curreentUser

  var currentUser = PFUser.currentUser()
  let imageData = UIImagePNGRepresentation(image)
  let imageFile = PFFile(name:"image.png", data:imageData)
  currentUser["profilePic"] = imageFile
  currentUser.saveInBackground()

Retrieve image from Parse curreentUser

var currentUser = PFUser.currentUser()
let userImageFile  = currentUser!["profilePic"] as? PFFile
userImageFile!.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in
         if error == nil {
            if let imageData = imageData {
               self.profileImage.image = UIImage(data:imageData)

                }
            }
       else
            {
                let errorString = error!.userInfo?["error"] as? String
                //there is no profile pic                
            }
        }

As per your question

PFQuery *query = [PFQuery queryWithClassName:@"Image"];
[query getObjectInBackgroundWithId:@"Y7ahcw9ZNR"
                         block:^(PFObject *textdu, NSError *error) {

     if (!error) {
          PFFile *imageFile = [textdu objectForKey:@"profilImage"];
          [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
              if (!error) {
                  UIImage *image = [UIImage imageWithData:data];
              }
          }];
     }
 }];


来源:https://stackoverflow.com/questions/31252860/download-image-from-parse-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!