Skip to content

Instantly share code, notes, and snippets.

@jtubert
Last active December 24, 2015 02:39
Show Gist options
  • Save jtubert/6731589 to your computer and use it in GitHub Desktop.
Save jtubert/6731589 to your computer and use it in GitHub Desktop.
Sketchio presentation - saving photos
UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(1152.0f, 1536.0f) interpolationQuality:kCGInterpolationHigh];
// JPEG to decrease file size and enable faster uploads & downloads
NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.8f);
PFFile* photoFile = [PFFile fileWithData:imageData];
// Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded
self.fileUploadBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}];
//NSLog(@"Requested background expiration task with id %d for Sketchio photo upload", self.fileUploadBackgroundTaskId);
[photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
//NSLog(@"Photo uploaded successfully");
} else {
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}
}];
// create a photo object
PFObject *photo = [PFObject objectWithClassName:kPAPPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kPAPPhotoUserKey];
[photo setObject:photoFile forKey:kPAPPhotoPictureKey];
// photos are public, but may only be modified by the user who uploaded them
PFACL *photoACL = [PFACL ACLWithUser:[PFUser currentUser]];
[photoACL setPublicReadAccess:YES];
photo.ACL = photoACL;
[photo saveInBackground];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment