Skip to content

Instantly share code, notes, and snippets.

@jkhowland
Created January 30, 2015 07:16
Show Gist options
  • Save jkhowland/5e1136f81a0c379fc4a2 to your computer and use it in GitHub Desktop.
Save jkhowland/5e1136f81a0c379fc4a2 to your computer and use it in GitHub Desktop.
OnTheLine-TakeSaveAccessPhotos
- (void)savePhoto:(UIImage *)photo completion:(void (^)(void))completion {
CGSize scaledSize = CGSizeMake(512, 512);
if (photo.size.width > photo.size.height) {
CGFloat ratio = photo.size.height / photo.size.width;
scaledSize.height = round(scaledSize.width * ratio);
} else {
CGFloat ratio = photo.size.width / photo.size.height;
scaledSize.width = round(scaledSize.height * ratio);
}
UIGraphicsBeginImageContext(scaledSize);
[photo drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)];
NSData *data = UIImageJPEGRepresentation(UIGraphicsGetImageFromCurrentImageContext(), 0.75);
UIGraphicsEndImageContext();
NSString *uniqueName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpeg"];
NSString *filePath = [self documentsPathWithFileName:uniqueName];
[data writeToFile:filePath atomically:YES]; //Write the file
NSMutableArray *photos = [[NSMutableArray alloc] initWithArray:self.photoURLs];
[photos addObject:uniqueName];
self.photoURLs = photos;
completion();
}
- (NSString *)documentsPathWithFileName:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:filename];
}
#pragma mark - Take Photo
- (IBAction)takePhoto:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.sourceType = sourceType;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerOriginalImage];
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Photo comment"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:nil];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showWithStatus:@"Uploading"];
});
UITextField *message = alertController.textFields.firstObject;
[[PhotosController sharedInstance] savePhoto:image message:message.text completion:^{
[SVProgressHUD showSuccessWithStatus:@"Posted"];
[self refresh:nil];
}];
}];
[alertController addAction:alertAction];
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:alertController animated:YES completion:nil];
}];
}
- (void)updateWithPhotoFileName:(NSString *)photoFileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSData *photoData = [NSData dataWithContentsOfFile:[documentsPath stringByAppendingPathComponent:photoFileName]];
UIImage *image = [UIImage imageWithData:photoData];
self.photoImageView.image = image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment