Created
January 3, 2015 02:11
-
-
Save ZacharyWeiner/3b2b0c84c4bc1b17c8df to your computer and use it in GitHub Desktop.
iOS7 Objective C Camera Code
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
- (IBAction)cameraBarButtonItemPressed:(UIBarButtonItem *)sender { | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ | |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
}else if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){ | |
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; | |
} | |
[self presentViewController:picker animated:true completion:nil]; | |
} | |
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
NSLog(@"Finished Picking"); | |
UIImage *image = info[UIImagePickerControllerEditedImage]; | |
if(!image){ | |
image = info[UIImagePickerControllerOriginalImage]; | |
} | |
[self.photos addObject:[self photoFromImage:image]]; | |
[self.collectionView reloadData]; | |
[self dismissViewControllerAnimated:true completion:nil]; | |
} | |
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker | |
{ | |
NSLog(@"Cancel Clicked"); | |
[self dismissViewControllerAnimated:true completion:nil]; | |
} | |
- (Photo *)photoFromImage:(UIImage *)image | |
{ | |
Photo *photo = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:[TWCoreDataHelper managedObjectContext]]; | |
photo.image = image; | |
photo.date = [NSDate date]; | |
photo.albumBook = self.album; | |
NSError *error = nil; | |
if(![[photo managedObjectContext] save:&error]){ | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Erro!" delegate:nil cancelButtonTitle:@"Cacel" otherButtonTitles:nil, nil]; | |
[alertView show]; | |
} | |
return photo; | |
} | |
@implementation TWPictureDataTransformer | |
- (Class)transformedValueClass | |
{ | |
return [NSData class]; | |
} | |
- (BOOL)allowsReververseTransformation | |
{ | |
return YES; | |
} | |
- (id)transformedValue:(id)value | |
{ | |
return UIImagePNGRepresentation(value); | |
} | |
- (id)reverseTransformedValue:(id)value | |
{ | |
UIImage *image = [UIImage imageWithData:value]; | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment