public
Created

  • Download Gist
TakePhoto.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
//
// DSPhotoController.m
// DoodleStash
//
// Created by Andrei Taraschuk on 11/24/12.
// Copyright (c) 2012 Andrei Taraschuk. All rights reserved.
//
 
#import "DSPhotoController.h"
#import "DSConfirmController.h"
 
@implementation DSPhotoController
 
@synthesize isNewDoodle, doodleImage;
 
- (void)viewDidLoad {
[super viewDidLoad];
 
// Custom backgorund
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_320x480.png"]];
self.view.backgroundColor = background;
}
 
 
/**
* Pass additional data to the new controller
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"confirmUploadSegue"]) {
// Get reference to the destination view controller
DSConfirmController *vc = [segue destinationViewController];
 
vc.doodleImage = self.doodleImage;
}
}
 
/**
* Choose image from the library
*/
- (IBAction)chooseFromLibrary:(id)sender {
 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
imagePicker.allowsEditing = YES;
 
[self presentViewController:imagePicker animated:YES completion:nil];
self.isNewDoodle = FALSE;
}
}
 
/**
* Take photo using camera control
*/
- (IBAction)takePhoto:(id)sender {
 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
 
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
 
[self presentViewController:imagePicker animated:YES completion:nil];
self.isNewDoodle = TRUE;
}
}
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
 
[self dismissViewControllerAnimated:YES completion:nil];
 
 
if ([mediaType isEqualToString:(NSString *) kUTTypeImage]) {
self.doodleImage = [info objectForKey:UIImagePickerControllerOriginalImage];
 
// Check to make sure the image is new
if (self.isNewDoodle == TRUE) {
UIImageWriteToSavedPhotosAlbum(self.doodleImage, self, @selector(image:finishedSavingWithError:contextInfo:), nil);
}
 
[self performSegueWithIdentifier:@"confirmUploadSegue" sender:self];
}
}
 
- (void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save failed" message:@"Failed to save image" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
 
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.