Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation ViewController
- - (IBAction)button:(id)sender {
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
- UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Camera"];
- [vc setModalPresentationStyle:UIModalPresentationFullScreen];
- [self presentModalViewController:vc animated:YES];
- }
- //camera.h//
- #import <UIKit/UIKit.h>
- #import <MobileCoreServices/MobileCoreServices.h>
- @interface Camera :UIViewController<UIImagePickerControllerDelegate,
- UINavigationControllerDelegate, UIPopoverControllerDelegate>
- {
- UIToolbar *toolbar;
- UIPopoverController *popoverController;
- UIImageView *imageView;
- BOOL newMedia;
- }
- @property (nonatomic, retain) IBOutlet UIImageView *imageView;
- @property (nonatomic, retain) UIPopoverController *popoverController;
- @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
- - (IBAction)useCamera: (id)sender;
- - (IBAction)useCameraRoll: (id)sender;
- @end
- //camera.m//
- #import "Camera.h"
- @interface Camera ()
- @end
- @implementation Camera
- @synthesize imageView, popoverController, toolbar;
- - (IBAction) useCamera: (id)sender
- {
- if ([UIImagePickerController isSourceTypeAvailable:
- UIImagePickerControllerSourceTypeCamera])
- {
- UIImagePickerController *imagePicker =
- [[UIImagePickerController alloc] init];
- imagePicker.delegate = self;
- imagePicker.sourceType =
- UIImagePickerControllerSourceTypeCamera;
- imagePicker.mediaTypes = [NSArray arrayWithObjects:
- (NSString *) kUTTypeImage,
- nil];
- imagePicker.allowsEditing = NO;
- [self presentModalViewController:imagePicker
- animated:YES];
- newMedia = YES;
- }
- }
- - (IBAction) useCameraRoll: (id)sender
- {
- if ([self.popoverController isPopoverVisible]) {
- [self.popoverController dismissPopoverAnimated:YES];
- } else {
- if ([UIImagePickerController isSourceTypeAvailable:
- UIImagePickerControllerSourceTypeSavedPhotosAlbum])
- {
- UIImagePickerController *imagePicker =
- [[UIImagePickerController alloc] init];
- imagePicker.delegate = self;
- imagePicker.sourceType =
- UIImagePickerControllerSourceTypePhotoLibrary;
- imagePicker.mediaTypes = [NSArray arrayWithObjects:
- (NSString *) kUTTypeImage,
- nil];
- imagePicker.allowsEditing = NO;
- self.popoverController = [[UIPopoverController alloc]
- initWithContentViewController:imagePicker];
- popoverController.delegate = self;
- [self.popoverController
- presentPopoverFromBarButtonItem:sender
- permittedArrowDirections:UIPopoverArrowDirectionAny
- animated:YES];
- newMedia = NO;
- }
- }
- }
- - (void)viewDidLoad
- {
- UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
- initWithTitle:@"Camera"
- style:UIBarButtonItemStyleBordered
- target:self
- action:@selector(useCamera:)];
- UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc]
- initWithTitle:@"Camera Roll"
- style:UIBarButtonItemStyleBordered
- target:self
- action:@selector(useCameraRoll:)];
- NSArray *items = [NSArray arrayWithObjects: cameraButton,
- cameraRollButton, nil];
- [toolbar setItems:items animated:NO];
- [super viewDidLoad];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- }
- -(void)imagePickerController:(UIImagePickerController *)picker
- didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- [self.popoverController dismissPopoverAnimated:true];
- NSString *mediaType = [info
- objectForKey:UIImagePickerControllerMediaType];
- [self dismissModalViewControllerAnimated:YES];
- if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
- UIImage *image = [info
- objectForKey:UIImagePickerControllerOriginalImage];
- imageView.image = image;
- if (newMedia)
- UIImageWriteToSavedPhotosAlbum(image,
- self,
- @selector(image:finishedSavingWithError:contextInfo:),
- nil);
- }
- else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
- {
- // Code here to support video if enabled
- }
- }
- -(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 dismissModalViewControllerAnimated:YES];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
- @end
Add Comment
Please, Sign In to add comment