To save image or video into parse database, parse has provided a class PFFile. It can also be used to save documents, music files or any binary data file (upto 10 megabytes only).
To save image first convert image to NSData, assign it to PFFile object and save it as follow:
UIImage *image = some image;
PFObject *newObject = [PFObject objectWithClassName:@"User_Images"];
[newObject setObject:[PFUser currentUser] forKey:@"user"];
NSData *data = UIImageJPEGRepresentation(image, 0.8);
PFFile *file = [PFFile fileWithName:@"image.png" data:data];
[object setObject:file forKey:@"userImage"];
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError * error){
// check for success and error here
}];
In above code we are saving an image to table "User_Image" where user column is equal to current user.
To read image from database use PFFile kile follow:
PFQuery *nextQuery = [PFQuery queryWithClassName:@"User_Images"];
[nextQuery whereKey:@"user" equalTo:[PFUser currentUser]];
[nextQuery findObjectsInBackgroundWithBlock:^(NSArray *result, NSError *error){
if (!error) {
if (result.count==0) {
callback(result, error);
}
for (PFObject *object in result) {
PFFile *file = object[@"userImage"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
}
}];
}
}else
callback(result, error);
}];
0 Comment(s)