Skip to content

Instantly share code, notes, and snippets.

@andreitr
Created January 11, 2013 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreitr/4515007 to your computer and use it in GitHub Desktop.
Save andreitr/4515007 to your computer and use it in GitHub Desktop.
//
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment