简体   繁体   English

从parse.com下载图像

[英]Download image from parse.com

I wanna download a image file form parse.com and displaying it in a ImageView "myImageView". 我想下载一个图像文件parse.com并将其显示在ImageView“ myImageView”中。 I create for that a class called "Image" and a column "profilImage" as a File Column. 我为此创建了一个名为“ Image”的类和一个“ profilImage”列作为“文件列”。 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? 我究竟做错了什么?

Something to try is using the getDataInBackgroundWithBlock PFFile function. 可以尝试使用getDataInBackgroundWithBlock PFFile函数。 getData is synchronous and could block the UI thread depending on how it is used. getData是同步的,并可能根据其使用方式阻止UI线程。

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

Save and Retrieve profilePic from Parse 从解析中保存和检索profilePic

-Objective-c -客观-c

save an image to Parse curreentUser 将图像保存到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 从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 将图像保存到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 从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];
              }
          }];
     }
 }];

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

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