我的应用程序名为“Draw Me”并使用 Parse.com。用户在UIImageView中绘制图像,应将其保存(上传)到Parse.com。有人可以建议怎么做吗?
问问题
15514 次
2 回答
23
Parse 有一个关于这个确切主题的 iOS 教程:https ://parse.com/tutorials/anypic
Christian 已经概述了如何保存图像本身,但我假设您也希望将其与 PFObject 相关联。基于他的答案(以保存为 jpeg 的示例)
// Convert to JPEG with 50% quality
NSData* data = UIImageJPEGRepresentation(imageView.image, 0.5f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data];
// Save the image to Parse
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// The image has now been uploaded to Parse. Associate it with a new object
PFObject* newPhotoObject = [PFObject objectWithClassName:@"PhotoObject"];
[newPhotoObject setObject:imageFile forKey:@"image"];
[newPhotoObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Saved");
}
else{
// Error
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
}];
于 2013-09-17T02:01:33.007 回答
5
以下是如何执行此操作:
UIImageView *imageView; // ...image view from previous code
NSData *imageData = UIImagePNGRepresentation(imageView.image);
PFFile *file = [PFFile fileWithData:imageData]
[file saveInBackground];
...并再次检索它:
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
imageView.image = [UIImage imageWithData:data];
}
}];
于 2013-09-17T01:35:25.933 回答