如何从UIImageView上传图片到Parse.com
我的应用程序被称为“画我”,并使用Parse.com。 用户在UIImageView中绘制图像,并将其保存(上传)到Parse.com 。 有人可以build议如何做到这一点?
parsing有关这个确切的主题的iOS教程: https : //parse.com/tutorials/anypic
Christian已经概述了如何保存图像本身,但是我也假定你也想把它和PFObject关联起来。 build立在他的答案(以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]); } }]; } }];
以下是如何去做这件事:
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]; } }];