如何从UIImageView上传图像到Parse.com

时间:2013-09-17 01:19:53

标签: ios objective-c uiimageview parse-platform

我的应用程序名为“Draw Me”并使用Parse.com。用户在 UIImageView 中绘制图像,并将其保存(上传)到 Parse.com 。有人可以建议怎么做吗?

2 个答案:

答案 0 :(得分: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]);
            }
        }];
    }
}];

答案 1 :(得分: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];
  }
}];