我想从parse.com下载图像文件并将其显示在ImageView" myImageView"中。我创建了一个名为" Image"和一个专栏" profilImage"作为文件列。我尝试使用该代码,但它仍然无法正常工作。
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];
}];
我做错了什么?
答案 0 :(得分:1)
要尝试的是使用getDataInBackgroundWithBlock PFFile函数。 getData是同步的,可能会阻止UI线程,具体取决于它的使用方式。
[theImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
myImageView.image = [UIImage imageWithData:data];
}];
答案 1 :(得分:1)
从Parse
保存并检索profilePic-Objective-C
将图片保存到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];
从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
将图片保存到Parse curreentUser
var currentUser = PFUser.currentUser()
let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name:"image.png", data:imageData)
currentUser["profilePic"] = imageFile
currentUser.saveInBackground()
从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
}
}
根据您的问题
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];
}
}];
}
}];