PureCamera.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*!
  2. @abstract
  3. Created by 孙凯峰 on 2016/10/18.
  4. */
  5. #define KScreenSize [UIScreen mainScreen].bounds.size
  6. #define KScreenwidth [UIScreen mainScreen].bounds.size.width
  7. #define KScreenheight [UIScreen mainScreen].bounds.size.height
  8. #define IsIphone6P KScreenSize.width==414
  9. #define IsIphone6 KScreenSize.width==375
  10. #define IsIphone5S KScreenSize.height==568
  11. #define IsIphone5 KScreenSize.height==568
  12. //456字体大小
  13. #define KIOS_Iphone456(iphone6p,iphone6,iphone5s,iphone5,iphone4s) (IsIphone6P?iphone6p:(IsIphone6?iphone6:((IsIphone5S||IsIphone5)?iphone5s:iphone4s)))
  14. //宽高
  15. #define KIphoneSize_Widith(iphone6) (IsIphone6P?1.104*iphone6:(IsIphone6?iphone6:((IsIphone5S||IsIphone5)?0.853*iphone6:0.853*iphone6)))
  16. #define KIphoneSize_Height(iphone6) (IsIphone6P?1.103*iphone6:(IsIphone6?iphone6:((IsIphone5S||IsIphone5)?0.851*iphone6:0.720*iphone6)))
  17. #import "PureCamera.h"
  18. #import "TOCropViewController.h"
  19. #import "LLSimpleCamera.h"
  20. @interface PureCamera ()<TOCropViewControllerDelegate>
  21. @property (strong, nonatomic) LLSimpleCamera *camera;
  22. @property (strong, nonatomic) UILabel *errorLabel;
  23. @property (strong, nonatomic) UIButton *snapButton;
  24. @property (strong, nonatomic) UIButton *switchButton;
  25. @property (strong, nonatomic) UIButton *flashButton;
  26. @property (strong, nonatomic) UIButton *backButton;
  27. @end
  28. @implementation PureCamera
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. //拍照按钮
  33. [self InitializeCamera];
  34. self.snapButton = [UIButton buttonWithType:UIButtonTypeCustom];
  35. self.snapButton.clipsToBounds = YES;
  36. self.snapButton.layer.cornerRadius =75 / 2.0f;
  37. [self.snapButton setImage:[UIImage imageNamed:@"PureCamera.bundle/cameraButton"] forState:UIControlStateNormal];
  38. [self.snapButton addTarget:self action:@selector(snapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  39. [self.view addSubview:self.snapButton];
  40. //闪关灯按钮
  41. self.flashButton = [UIButton buttonWithType:UIButtonTypeSystem];
  42. self.flashButton.tintColor = [UIColor whiteColor];
  43. // UIImage *image = [UIImage imageNamed:@"PureCamera.bundle/camera-flash.png"];
  44. [self.flashButton setImage:[UIImage imageNamed:@"PureCamera.bundle/camera-flash"] forState:UIControlStateNormal];
  45. self.flashButton.imageEdgeInsets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
  46. [self.flashButton addTarget:self action:@selector(flashButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  47. [self.view addSubview:self.flashButton];
  48. if([LLSimpleCamera isFrontCameraAvailable] && [LLSimpleCamera isRearCameraAvailable]) {
  49. //摄像头转换按钮
  50. self.switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  51. // self.switchButton.tintColor = [UIColor whiteColor];
  52. [self.switchButton setImage:[UIImage imageNamed:@"PureCamera.bundle/swapButton"] forState:UIControlStateNormal];
  53. [self.switchButton addTarget:self action:@selector(switchButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  54. [self.view addSubview:self.switchButton];
  55. //返回按钮
  56. self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  57. [self.backButton setImage:[UIImage imageNamed:@"PureCamera.bundle/closeButton"] forState:UIControlStateNormal];
  58. [self.backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  59. [self.view addSubview:self.backButton];
  60. }
  61. }
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [super viewWillAppear:animated];
  65. self.view.backgroundColor = [UIColor blackColor];
  66. [self.navigationController setNavigationBarHidden:YES animated:NO];
  67. // snap button to capture image
  68. //判断前后摄像头是否可用
  69. // start the camera
  70. [self.camera start];
  71. }
  72. #pragma mark ------------- 初始化相机--------------
  73. -(void)InitializeCamera{
  74. CGRect screenRect = self.view.frame;
  75. // 创建一个相机
  76. self.camera = [[LLSimpleCamera alloc] initWithQuality:AVCaptureSessionPresetHigh position:LLCameraPositionRear];
  77. // attach to a view controller
  78. [self.camera attachToViewController:self withFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
  79. self.camera.fixOrientationAfterCapture = NO;
  80. // take the required actions on a device change
  81. __weak typeof(self) weakSelf = self;
  82. [self.camera setOnDeviceChange:^(LLSimpleCamera *camera, AVCaptureDevice * device) {
  83. NSLog(@"Device changed.");
  84. // device changed, check if flash is available
  85. if([camera isFlashAvailable]) {
  86. weakSelf.flashButton.hidden = NO;
  87. if(camera.flash == LLCameraFlashOff) {
  88. weakSelf.flashButton.selected = NO;
  89. }
  90. else {
  91. weakSelf.flashButton.selected = YES;
  92. }
  93. }
  94. else {
  95. weakSelf.flashButton.hidden = YES;
  96. }
  97. }];
  98. [self.camera setOnError:^(LLSimpleCamera *camera, NSError *error) {
  99. NSLog(@"Camera error: %@", error);
  100. if([error.domain isEqualToString:LLSimpleCameraErrorDomain]) {
  101. if(error.code == LLSimpleCameraErrorCodeCameraPermission) {
  102. if(weakSelf.errorLabel) {
  103. [weakSelf.errorLabel removeFromSuperview];
  104. }
  105. UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  106. label.text = @"未获取相机权限";
  107. label.numberOfLines = 2;
  108. label.lineBreakMode = NSLineBreakByWordWrapping;
  109. label.backgroundColor = [UIColor clearColor];
  110. label.font = [UIFont fontWithName:@"AvenirNext-DemiBold" size:13.0f];
  111. label.textColor = [UIColor whiteColor];
  112. label.textAlignment = NSTextAlignmentCenter;
  113. [label sizeToFit];
  114. label.center = CGPointMake(screenRect.size.width / 2.0f, screenRect.size.height / 2.0f);
  115. weakSelf.errorLabel = label;
  116. [weakSelf.view addSubview:weakSelf.errorLabel];
  117. }
  118. }
  119. }];
  120. }
  121. /* camera button methods */
  122. - (void)switchButtonPressed:(UIButton *)button
  123. {
  124. [self.camera togglePosition];
  125. }
  126. -(void)backButtonPressed:(UIButton *)button{
  127. [self dismissViewControllerAnimated:YES completion:nil];
  128. }
  129. - (NSURL *)applicationDocumentsDirectory
  130. {
  131. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  132. }
  133. - (void)flashButtonPressed:(UIButton *)button
  134. {
  135. if(self.camera.flash == LLCameraFlashOff) {
  136. BOOL done = [self.camera updateFlashMode:LLCameraFlashOn];
  137. if(done) {
  138. self.flashButton.selected = YES;
  139. self.flashButton.tintColor = [UIColor yellowColor];
  140. }
  141. }
  142. else {
  143. BOOL done = [self.camera updateFlashMode:LLCameraFlashOff];
  144. if(done) {
  145. self.flashButton.selected = NO;
  146. self.flashButton.tintColor = [UIColor whiteColor];
  147. }
  148. }
  149. }
  150. #pragma mark -------------拍照--------------
  151. - (void)snapButtonPressed:(UIButton *)button
  152. {
  153. __weak typeof(self) weakSelf = self;
  154. // 去拍照
  155. [self.camera capture:^(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error) {
  156. NSLog(@"拍照结束");
  157. if(!error) {
  158. TOCropViewController *cropController = [[TOCropViewController alloc] initWithImage:image];
  159. cropController.delegate = self;
  160. [weakSelf presentViewController:cropController animated:YES completion:nil];
  161. }
  162. else {
  163. NSLog(@"An error has occured: %@", error);
  164. }
  165. } exactSeenImage:YES];
  166. }
  167. - (void)viewWillLayoutSubviews
  168. {
  169. [super viewWillLayoutSubviews];
  170. self.camera.view.frame=self.view.frame;
  171. self.snapButton.frame=CGRectMake((KScreenwidth-KIphoneSize_Widith(75))/2, KScreenheight-KIphoneSize_Widith(90), KIphoneSize_Widith(75), KIphoneSize_Widith(75));
  172. self.flashButton.frame=CGRectMake((KScreenwidth-KIphoneSize_Widith(36))/2, 25, KIphoneSize_Widith(36), KIphoneSize_Widith(44));
  173. self.switchButton.frame=CGRectMake(KScreenwidth-50, KScreenheight-KIphoneSize_Widith(75), KIphoneSize_Widith(45), KIphoneSize_Widith(45));
  174. self.backButton.frame=CGRectMake(5, KScreenheight-KIphoneSize_Widith(75), KIphoneSize_Widith(45), KIphoneSize_Widith(45));
  175. }
  176. - (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle{
  177. self.view.alpha = 0;
  178. self.fininshcapture(image);
  179. [self dismissViewControllerAnimated:NO completion:^{
  180. }];
  181. [self dismissViewControllerAnimated:NO completion:^{
  182. }];
  183. }
  184. - (BOOL)prefersStatusBarHidden
  185. {
  186. return YES;
  187. }
  188. - (void)didReceiveMemoryWarning
  189. {
  190. [super didReceiveMemoryWarning];
  191. }
  192. @end