2015-02-11 60 views
0

感谢@stee的帮助,我能够将图片上传到parse.com中的正确课程。现在我无法在用于上传的相同uiimageview中显示相同的图像。从Picker上解析图片上传

在@stee和@Joey的帮助下,我能够将下面的代码添加到我的viewdidload中。现在,我收到一条警告,提示*图像未使用的变量,当我编译并运行时,我可以上传,但只要我离开控制器,图像就不会在那里。

- (void)viewDidLoad { 
[super viewDidLoad]; 

PFFile * myPFFile = [[PFUser currentUser] objectForKey:@"ProfilePicture"]; 
[myPFFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
    if (!error) { 
     UIImage *image = [UIImage imageWithData:data]; 
     // image can now be set on a UIImageView 
    } 
}]; 
} 

如果它可以帮助你明白我做了什么,下面是我用来上传照片

PFObject *User = [PFUser currentUser]; 
NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 0.8); 
NSString *filename = [NSString stringWithFormat:@"%@.png", _username.text]; 
PFFile *imageFile = [PFFile fileWithName:filename data:imageData]; 
[User setObject:imageFile forKey:@"ProfilePicture"]; 

代码有什么,我从我的viewDidLoad中缺少什么?我是否缺少我的.h文件中的任何内容?

在我的.h文件中我有那样的uiimageview;

@property (strong, nonatomic) IBOutlet UIImageView *imageView; 

我非常感谢任何进一步的帮助,使这成为可能。

+0

@Stee任何想法,我可能在这里做错了吗? – 2015-02-16 17:11:48

回答

0

将图像保存到上传的用户后,可以通过[User objectForKey:@“ProfilePicture”]以PFFile的形式进行访问。 然后,您可以使用PFFile loadInBackground函数之一在图像视图上设置图像。

我在代码的其他地方发现了一个问题,您保存图像的用户每次都是新的PFUser。替换[PFObject objectWithClassName:@“User”];与[PFUser currentUser];

+0

Tnx的捕获。我注意到我创建了一个新类并上传了数百万次,而不是将其添加到当前用户。我仍然试图弄清楚如何在添加后显示图像。 – 2015-02-11 17:16:52

+0

Joey Clover的答案显示了从文件对象获取图像的代码。您只需要使用PFFile * myPFFile = [[PFUser currentUser] objectForKey:@“ProfilePicture”]从viewDidLoad中获取当前用户的PFFile;然后使用他答案中的代码并在图像视图上设置图像。 – Stee 2015-02-11 17:58:46

0

您可以将每次在控制器中“viewDidLoad”时选中的图像保存到磁盘,或从Parse请求用户配置文件。

使用[_imageView.image writeToFile:myFilePath atomically:YES];将图像保存到磁盘可以节省网络请求,但如果用户可以使用应用程序以外的方法更改图片,则可能不是最佳选择。

关于每次请求图片,您可以在[PFUser currentUser]上执行“获取”。一旦你完成提取,你可以从[PFUser currentUser]["ProfilePicture"]得到PFFile *file。一旦你从你的PFUser得到了PFFile,进而你就可以把它转换:

[myPFFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
    if (!error) { 
    UIImage *image = [UIImage imageWithData:data]; 
    // image can now be set on a UIImageView  
    } 
}]; 

这是对的最好方式,我可以用你给我的信息,解释它!