Last active
December 24, 2015 02:39
-
-
Save jtubert/6731589 to your computer and use it in GitHub Desktop.
Sketchio presentation - saving photos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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