Guest User

Untitled

a guest
Jan 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. @implementation ViewController
  2. - (IBAction)button:(id)sender {
  3.  
  4. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
  5. UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Camera"];
  6. [vc setModalPresentationStyle:UIModalPresentationFullScreen];
  7. [self presentModalViewController:vc animated:YES];
  8.  
  9.  
  10. }
  11.  
  12. //camera.h//
  13.  
  14. #import <UIKit/UIKit.h>
  15. #import <MobileCoreServices/MobileCoreServices.h>
  16. @interface Camera :UIViewController<UIImagePickerControllerDelegate,
  17. UINavigationControllerDelegate, UIPopoverControllerDelegate>
  18.  
  19. {
  20. UIToolbar *toolbar;
  21. UIPopoverController *popoverController;
  22. UIImageView *imageView;
  23. BOOL newMedia;
  24. }
  25.  
  26. @property (nonatomic, retain) IBOutlet UIImageView *imageView;
  27. @property (nonatomic, retain) UIPopoverController *popoverController;
  28. @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
  29.  
  30. - (IBAction)useCamera: (id)sender;
  31. - (IBAction)useCameraRoll: (id)sender;
  32.  
  33. @end
  34.  
  35. //camera.m//
  36.  
  37. #import "Camera.h"
  38.  
  39. @interface Camera ()
  40.  
  41. @end
  42.  
  43. @implementation Camera
  44. @synthesize imageView, popoverController, toolbar;
  45.  
  46. - (IBAction) useCamera: (id)sender
  47. {
  48. if ([UIImagePickerController isSourceTypeAvailable:
  49. UIImagePickerControllerSourceTypeCamera])
  50. {
  51. UIImagePickerController *imagePicker =
  52. [[UIImagePickerController alloc] init];
  53. imagePicker.delegate = self;
  54. imagePicker.sourceType =
  55. UIImagePickerControllerSourceTypeCamera;
  56. imagePicker.mediaTypes = [NSArray arrayWithObjects:
  57. (NSString *) kUTTypeImage,
  58. nil];
  59. imagePicker.allowsEditing = NO;
  60. [self presentModalViewController:imagePicker
  61. animated:YES];
  62. newMedia = YES;
  63. }
  64. }
  65.  
  66. - (IBAction) useCameraRoll: (id)sender
  67. {
  68. if ([self.popoverController isPopoverVisible]) {
  69. [self.popoverController dismissPopoverAnimated:YES];
  70. } else {
  71. if ([UIImagePickerController isSourceTypeAvailable:
  72. UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  73. {
  74. UIImagePickerController *imagePicker =
  75. [[UIImagePickerController alloc] init];
  76. imagePicker.delegate = self;
  77. imagePicker.sourceType =
  78. UIImagePickerControllerSourceTypePhotoLibrary;
  79. imagePicker.mediaTypes = [NSArray arrayWithObjects:
  80. (NSString *) kUTTypeImage,
  81. nil];
  82. imagePicker.allowsEditing = NO;
  83.  
  84. self.popoverController = [[UIPopoverController alloc]
  85. initWithContentViewController:imagePicker];
  86.  
  87. popoverController.delegate = self;
  88.  
  89. [self.popoverController
  90. presentPopoverFromBarButtonItem:sender
  91. permittedArrowDirections:UIPopoverArrowDirectionAny
  92. animated:YES];
  93. newMedia = NO;
  94. }
  95. }
  96. }
  97.  
  98. - (void)viewDidLoad
  99. {
  100. UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
  101. initWithTitle:@"Camera"
  102. style:UIBarButtonItemStyleBordered
  103. target:self
  104. action:@selector(useCamera:)];
  105. UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc]
  106. initWithTitle:@"Camera Roll"
  107. style:UIBarButtonItemStyleBordered
  108. target:self
  109. action:@selector(useCameraRoll:)];
  110. NSArray *items = [NSArray arrayWithObjects: cameraButton,
  111. cameraRollButton, nil];
  112. [toolbar setItems:items animated:NO];
  113. [super viewDidLoad];
  114. }
  115.  
  116. - (void)viewDidUnload
  117. {
  118. [super viewDidUnload];
  119. // Release any retained subviews of the main view.
  120. }
  121.  
  122. -(void)imagePickerController:(UIImagePickerController *)picker
  123. didFinishPickingMediaWithInfo:(NSDictionary *)info
  124. {
  125. [self.popoverController dismissPopoverAnimated:true];
  126. NSString *mediaType = [info
  127. objectForKey:UIImagePickerControllerMediaType];
  128. [self dismissModalViewControllerAnimated:YES];
  129. if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
  130. UIImage *image = [info
  131. objectForKey:UIImagePickerControllerOriginalImage];
  132.  
  133. imageView.image = image;
  134. if (newMedia)
  135. UIImageWriteToSavedPhotosAlbum(image,
  136. self,
  137. @selector(image:finishedSavingWithError:contextInfo:),
  138. nil);
  139. }
  140. else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
  141. {
  142. // Code here to support video if enabled
  143. }
  144. }
  145.  
  146. -(void)image:(UIImage *)image
  147. finishedSavingWithError:(NSError *)error
  148. contextInfo:(void *)contextInfo
  149. {
  150. if (error) {
  151. UIAlertView *alert = [[UIAlertView alloc]
  152. initWithTitle: @"Save failed"
  153. message: @"Failed to save image"
  154. delegate: nil
  155. cancelButtonTitle:@"OK"
  156. otherButtonTitles:nil];
  157. [alert show];
  158. }
  159. }
  160.  
  161. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  162. {
  163. [self dismissModalViewControllerAnimated:YES];
  164. }
  165.  
  166. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  167. {
  168. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  169. if (self) {
  170. // Custom initialization
  171. }
  172. return self;
  173. }
  174. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  175. {
  176. return YES;
  177. }
  178.  
  179. @end
Add Comment
Please, Sign In to add comment