RACameraViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //
  2. // RACameraViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/5.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RACameraViewController.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. #import "RATakePhotoPreviewController.h"
  11. #import <CoreMotion/CoreMotion.h>
  12. @interface RACameraViewController ()<AVCapturePhotoCaptureDelegate>
  13. @property (nonatomic,strong) IBOutlet UIView *previewContainer;
  14. @property (nonatomic,strong) IBOutlet UIView *controlContanier;
  15. @property (strong, nonatomic) IBOutlet UIButton *takePhotoBtn;
  16. @property (strong, nonatomic) IBOutlet UIButton *backBtn;
  17. @property (nonatomic,assign) BOOL barHidden;
  18. #pragma mark - Camera
  19. @property (nonatomic,strong) AVCaptureDevice *captureDevice;
  20. @property (nonatomic,strong) AVCaptureSession *captureSession;
  21. @property (nonatomic,strong) AVCaptureDeviceInput *captureInput;
  22. //@property (nonatomic,strong) AVCaptureStillImageOutput *captureOutput;
  23. @property (nonatomic,strong) AVCapturePhotoSettings *photoSetting;
  24. @property (nonatomic,strong) AVCapturePhotoOutput* photoOutput;
  25. @property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
  26. @property (nonatomic,assign) BOOL cameraInitial;
  27. @property (nonatomic,assign) AVLayerVideoGravity gravity;
  28. @property (strong, nonatomic) CMMotionManager *motionManager;
  29. @property (assign, nonatomic) UIDeviceOrientation orientationLast;
  30. @end
  31. @implementation RACameraViewController
  32. -(BOOL)shouldAutorotate {
  33. return NO;
  34. }
  35. - (void)initMotionManager
  36. {
  37. if (_motionManager == nil) {
  38. _motionManager = [[CMMotionManager alloc] init];
  39. }
  40. // 提供设备运动数据到指定的时间间隔
  41. _motionManager.deviceMotionUpdateInterval = .3;
  42. if (_motionManager.deviceMotionAvailable) { // 确定是否使用任何可用的态度参考帧来决定设备的运动是否可用
  43. [_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
  44. withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
  45. if (!error) {
  46. [self outputAccelertionData:accelerometerData.acceleration];
  47. }
  48. else{
  49. NSLog(@"%@", error);
  50. }
  51. }];
  52. }else
  53. {
  54. [self setMotionManager:nil];
  55. }
  56. }
  57. - (void)outputAccelertionData:(CMAcceleration)acceleration{
  58. UIDeviceOrientation orientationNew;
  59. // NSLog(@"x %f y %f z %f",acceleration.x,acceleration.y, acceleration.z);
  60. if (acceleration.x >= 0.75) {
  61. orientationNew = UIDeviceOrientationLandscapeRight;
  62. }
  63. else if (acceleration.x <= -0.75) {
  64. orientationNew = UIDeviceOrientationLandscapeLeft;
  65. }
  66. else if (acceleration.y <= -0.75) {
  67. orientationNew = UIDeviceOrientationPortrait;
  68. }
  69. else if (acceleration.y >= 0.75) {
  70. orientationNew = UIDeviceOrientationPortraitUpsideDown;
  71. }
  72. else {
  73. // Consider same as last time
  74. return;
  75. }
  76. if (orientationNew == self.orientationLast)
  77. return;
  78. self.orientationLast = orientationNew;
  79. // [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orientationNew] forKey:@"orientation"];
  80. // CGRect frame = self.previewContainer.bounds;
  81. // [self resetPreview:frame];
  82. }
  83. + (NSString *)storyboardID {
  84. return NSStringFromClass([self class]);
  85. }
  86. + (instancetype)viewControllerFromStoryboard {
  87. RACameraViewController *cameraVC = [[UIStoryboard storyboardWithName:@"Camera" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  88. cameraVC.takeMode = RACameraTakeModeTakeOnce;
  89. cameraVC.gravity = AVLayerVideoGravityResizeAspectFill;
  90. return cameraVC;
  91. }
  92. + (instancetype)showCameraFromViewController:(UIViewController *)from withTakeMode:(RACameraTakeMode)mode videoGravity:(AVLayerVideoGravity)gravity completion:(void(^)(UIImage *image))completion {
  93. RACameraViewController *vc = [self viewControllerFromStoryboard];
  94. vc.takeMode = mode;
  95. if (!gravity) {
  96. gravity = AVLayerVideoGravityResizeAspectFill;
  97. }
  98. vc.gravity = gravity;
  99. vc.completion = completion;
  100. return vc;
  101. }
  102. - (void)viewDidLoad {
  103. [super viewDidLoad];
  104. // Do any additional setup after loading the view.
  105. [self initMotionManager];
  106. UIImage *normal_img = [self.class imageWithColor:[UIColor redColor] Size:CGSizeMake(60, 60)];
  107. UIImage *highlight_img = [self.class imageWithColor:[UIColor whiteColor] Size:CGSizeMake(60, 60)];
  108. [self.takePhotoBtn setImage:normal_img forState:UIControlStateNormal];
  109. [self.takePhotoBtn setImage:highlight_img forState:UIControlStateHighlighted];
  110. // [self initCapture];
  111. // [self initTakePicture];
  112. if ([self camerAuthorization]) {
  113. [self initCapture];
  114. [self initTakePicture];
  115. } else {
  116. // fix:第一次打开相机的时候授权成功后,未初始化相机
  117. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. if (granted) {
  120. [self initCapture];
  121. [self initTakePicture];
  122. CGRect frame = self.previewContainer.bounds;
  123. [self resetPreview:CGRectOffset(frame, CGRectGetWidth(frame), 0)];
  124. [UIView animateWithDuration:0.3 animations:^{
  125. self.previewLayer.frame = frame;
  126. }];
  127. if (self.cameraInitial && !self.captureSession.isRunning) {
  128. [self.captureSession startRunning];
  129. }
  130. } else {
  131. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  132. NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
  133. if (!appName) {
  134. appName = [infoDict objectForKey:@"CFBundleName"];
  135. }
  136. __weak typeof(self) weakSelf = self;
  137. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Camera access denied, please change %@ setting, allow App use camera. (setting -> privacy -> camera enable %@)",[UIDevice currentDevice].model,appName] preferredStyle:UIAlertControllerStyleAlert];
  138. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  139. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  140. }];
  141. [alert addAction:action];
  142. [self presentViewController:alert animated:YES completion:nil];
  143. }
  144. });
  145. }];
  146. }
  147. /**
  148. * 管理导航条
  149. * 在viewDidload的时候隐藏导航条
  150. * 恢复导航条的显示/隐藏有两个地方:1.退出的时候;2.确认使用照片的时候
  151. */
  152. if (self.navigationController) {
  153. BOOL navBarHidden = self.navigationController.navigationBar.hidden;
  154. self.barHidden = navBarHidden;
  155. if (!navBarHidden) {
  156. [self.navigationController setNavigationBarHidden:YES animated:YES];
  157. }
  158. }
  159. }
  160. - (void)didReceiveMemoryWarning {
  161. [super didReceiveMemoryWarning];
  162. // Dispose of any resources that can be recreated.
  163. }
  164. - (void)viewWillAppear:(BOOL)animated {
  165. [super viewWillAppear:animated];
  166. if (self.cameraInitial && !self.captureSession.isRunning) {
  167. [self.captureSession startRunning];
  168. }
  169. }
  170. - (void)viewWillDisappear:(BOOL)animated {
  171. [super viewWillDisappear:animated];
  172. if ([self.captureSession isRunning]) {
  173. [self.captureSession stopRunning];
  174. }
  175. }
  176. - (BOOL)prefersStatusBarHidden {
  177. return YES;
  178. }
  179. - (void)viewDidLayoutSubviews {
  180. [super viewDidLayoutSubviews];
  181. // 强制布局一次,不然 takePhotoBtn frame还没发生改变
  182. [self.controlContanier layoutIfNeeded];
  183. if ([self camerAuthorization] && self.cameraInitial) {
  184. CGRect frame = self.previewContainer.bounds;
  185. BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown;
  186. // aspect ratio 3:4
  187. if (self.gravity == AVLayerVideoGravityResizeAspect) {
  188. CGRect backBtnFrame = [self.previewContainer convertRect:self.backBtn.frame fromView:self.controlContanier];
  189. CGRect captureBtnFrame = [self.previewContainer convertRect:self.takePhotoBtn.frame fromView:self.controlContanier];
  190. if (isPortrait) {
  191. CGFloat left = 0;
  192. CGFloat top = CGRectGetMaxY(backBtnFrame);
  193. CGFloat bottom = CGRectGetMinY(captureBtnFrame);
  194. CGFloat width = CGRectGetWidth(self.controlContanier.bounds);
  195. CGFloat height = bottom - top;
  196. frame = CGRectMake(left, top, width, height);
  197. } else {
  198. CGFloat left = CGRectGetMaxX(backBtnFrame);
  199. CGFloat right = CGRectGetMinX(captureBtnFrame);
  200. CGFloat top = 0;
  201. CGFloat width = right - left;
  202. CGFloat bottom = CGRectGetHeight(self.controlContanier.bounds);
  203. CGFloat height = bottom - top;
  204. frame = CGRectMake(left, top, width, height);
  205. }
  206. }
  207. [self resetPreview:frame];
  208. }
  209. }
  210. #pragma mark - Capture
  211. - (void)resetPreview:(CGRect)frame {
  212. self.previewLayer.frame = frame;
  213. // if (self.previewLayer.connection.isVideoOrientationSupported) {
  214. // self.previewLayer.connection.videoOrientation = [self captureVideoOrientation];
  215. // }
  216. //
  217. // if ([self.captureOutput connectionWithMediaType:AVMediaTypeVideo].isVideoOrientationSupported) {
  218. // [self.captureOutput connectionWithMediaType:AVMediaTypeVideo].videoOrientation = [self captureVideoOrientation];
  219. // }
  220. //
  221. }
  222. - (BOOL)camerAuthorization {
  223. AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  224. if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
  225. return NO;
  226. }
  227. return YES;
  228. }
  229. - (void)initCapture {
  230. self.photoSetting= [AVCapturePhotoSettings photoSettings];
  231. self.cameraInitial = NO;
  232. if (![self camerAuthorization]) {
  233. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  234. NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
  235. if (!appName) {
  236. appName = [infoDict objectForKey:@"CFBundleName"];
  237. }
  238. __weak typeof(self) weakSelf = self;
  239. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Camera access denied, please change %@ setting, allow App use camera. (setting -> privacy -> camera enable %@)",[UIDevice currentDevice].model,appName] preferredStyle:UIAlertControllerStyleAlert];
  240. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  241. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  242. }];
  243. [alert addAction:action];
  244. [self presentViewController:alert animated:YES completion:nil];
  245. return;
  246. }
  247. self.captureSession = [[AVCaptureSession alloc] init];
  248. self.captureDevice = [self videoDevicePosition:AVCaptureDevicePositionBack];
  249. if (!self.captureDevice) {
  250. NSLog(@"there is no capture device while init camera");
  251. return;
  252. }
  253. NSError *error;
  254. self.captureInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:&error];
  255. if (error) {
  256. NSLog(@"init camera error: %@",error);
  257. return;
  258. }
  259. [self.captureSession beginConfiguration];
  260. if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
  261. _captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
  262. }
  263. // if ([self.captureDevice isFlashAvailable] && [_captureDevice isFlashModeSupported:AVCaptureFlashModeAuto]) {
  264. if ([self.captureDevice isFlashAvailable] && [self.photoOutput supportedFlashModes]) {
  265. NSError *configErr;
  266. [self.captureDevice lockForConfiguration:&configErr];
  267. // AVCapturePhotoSettings.flashMode
  268. self.photoSetting.flashMode =AVCaptureFlashModeAuto;
  269. // [self.captureDevice setFlashMode:AVCaptureFlashModeAuto];
  270. [self.captureDevice unlockForConfiguration];
  271. }
  272. // if ([self.captureDevice isAdjustingWhiteBalance] && [self.captureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
  273. // NSError *configErr;
  274. // [self.captureDevice lockForConfiguration:&configErr];
  275. // [self.captureDevice setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
  276. // [self.captureDevice unlockForConfiguration];
  277. // }
  278. if ([self.captureSession canAddInput:self.captureInput]) {
  279. [self.captureSession addInput:self.captureInput];
  280. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
  281. // self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
  282. // } else {
  283. // if ([self.captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
  284. // self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  285. // }
  286. // }
  287. } else {
  288. NSLog(@"init camera can't add input");
  289. return;
  290. }
  291. [self.captureSession commitConfiguration];
  292. self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
  293. self.previewLayer.videoGravity = self.gravity;
  294. [self.previewContainer.layer addSublayer:self.previewLayer];
  295. self.cameraInitial = YES;
  296. }
  297. - (void)initTakePicture {
  298. if (!self.cameraInitial) {
  299. return;
  300. }
  301. // self.captureOutput = [[AVCaptureStillImageOutput alloc] init];
  302. self.photoOutput = [AVCapturePhotoOutput new];
  303. // NSDictionary *setting = @{AVVideoCodecKey:AVVideoCodecJPEG};
  304. // AVVideoCodecTypeJPEG
  305. // self.photoSetting.cod
  306. // [self.captureOutput setOutputSettings:setting];
  307. //
  308. if ([self.captureSession canAddOutput:self.photoOutput]) {
  309. [self.captureSession addOutput:self.photoOutput];
  310. }
  311. // AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  312. // if (connection.isVideoOrientationSupported) { // addOutput之后判断,否则一直都是false
  313. // connection.videoOrientation = [self captureVideoOrientation];
  314. // }
  315. }
  316. - (AVCaptureDevice *) videoDevicePosition:(AVCaptureDevicePosition)position {
  317. // NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  318. AVCaptureDeviceDiscoverySession * _Nonnull cameras = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position: AVCaptureDevicePositionUnspecified];//AVCaptureDevicePositionFront & AVCaptureDevicePositionBack
  319. // //获取当前摄像头方向
  320. // AVCaptureDevicePosition currentPosition = self.captureDeviceInput.device.position;
  321. // AVCaptureDevicePosition toPosition = AVCaptureDevicePositionUnspecified;
  322. // if (currentPosition == AVCaptureDevicePositionBack || currentPosition == AVCaptureDevicePositionUnspecified) {
  323. // toPosition = AVCaptureDevicePositionFront;
  324. // }else{
  325. // toPosition = AVCaptureDevicePositionBack;
  326. // }
  327. // NSArray* captureDeviceArray = [cameras.devices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"position == %d",toPosition]];
  328. // if (captureDeviceArray.count == 0) {
  329. // return [self throw_errorWithDomain:@"VideoCapture: reverseCamera failed! get new camera failed!"];
  330. // }
  331. for (AVCaptureDevice *device in cameras.devices) {
  332. if ([device position] == position) {
  333. return device;
  334. }
  335. }
  336. return nil;
  337. }
  338. - (AVCaptureVideoOrientation)captureVideoOrientation {
  339. AVCaptureVideoOrientation result;
  340. // UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  341. switch (self.orientationLast) {
  342. case UIDeviceOrientationPortrait:
  343. case UIDeviceOrientationFaceUp:
  344. case UIDeviceOrientationFaceDown:
  345. result = AVCaptureVideoOrientationPortrait;
  346. break;
  347. case UIDeviceOrientationPortraitUpsideDown:
  348. //如果这里设置成AVCaptureVideoOrientationPortraitUpsideDown,则视频方向和拍摄时的方向是相反的。
  349. result = AVCaptureVideoOrientationPortraitUpsideDown;
  350. break;
  351. case UIDeviceOrientationLandscapeLeft:
  352. result = AVCaptureVideoOrientationLandscapeRight;
  353. break;
  354. case UIDeviceOrientationLandscapeRight:
  355. result = AVCaptureVideoOrientationLandscapeLeft;
  356. break;
  357. default:
  358. result = AVCaptureVideoOrientationPortrait;
  359. break;
  360. }
  361. return result;
  362. }
  363. #pragma mark - Action
  364. - (IBAction)takePictureBtnClick:(UIButton *)sender {
  365. // UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  366. // NSLog(@"UIDeviceOrientation %ld",deviceOrientation);
  367. // [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  368. if (!self.cameraInitial) {
  369. return;
  370. }
  371. sender.enabled = false;
  372. // AVCaptureConnection *connection = [self.captureOutput connectionWithMediaType:AVMediaTypeVideo];
  373. //
  374. // if (connection.isVideoOrientationSupported) { // addOutput之后判断,否则一直都是false
  375. // connection.videoOrientation = [self captureVideoOrientation];
  376. // }
  377. //
  378. // NSLog(@"capture orientation %ld",connection.videoOrientation);
  379. ;
  380. // __weak typeof(self) weakSelf = self;
  381. [self.photoOutput capturePhotoWithSettings:self.photoSetting delegate:self];
  382. //
  383. // [self.captureOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {
  384. //
  385. // if (imageDataSampleBuffer) {
  386. // NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  387. // UIImage *image=[UIImage imageWithData:imageData];
  388. //
  389. // RATakePhotoPreviewController *preVC = [RATakePhotoPreviewController viewControllerFromStoryboard];
  390. // preVC.photoHandler = ^(UIImage *img){
  391. // if (weakSelf) {
  392. // __strong typeof(weakSelf) strongSelf = weakSelf;
  393. // if (strongSelf.completion) {
  394. // strongSelf.completion(img);
  395. // }
  396. // }
  397. // };
  398. // preVC.preImage = image;
  399. // if (weakSelf.takeMode == RACameraTakeModeTakeOnce) {
  400. // preVC.popTo = weakSelf.fromVC;
  401. // preVC.barHidden = weakSelf.barHidden;
  402. // }
  403. // [weakSelf.navigationController pushViewController:preVC animated:YES];
  404. //
  405. // } else {
  406. // NSLog(@"take picture failed: %@",error);
  407. // }
  408. // sender.enabled = true;
  409. // }];
  410. }
  411. - (IBAction)returnButtonClick:(UIButton *)sender {
  412. if (self.navigationController) {
  413. [self.navigationController popViewControllerAnimated:YES];
  414. } else {
  415. [self dismissViewControllerAnimated:YES completion:nil];
  416. }
  417. if (self.navigationController) {
  418. [self.navigationController setNavigationBarHidden:self.barHidden animated:YES];
  419. }
  420. }
  421. #pragma mark - Utils
  422. + (UIImage *)imageWithColor:(UIColor *)color Size:(CGSize)size {
  423. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  424. CGContextRef ctx = UIGraphicsGetCurrentContext();
  425. CGContextAddEllipseInRect(ctx, CGRectMake(5, 5, size.width - 10, size.height - 10));
  426. CGContextSetFillColorWithColor(ctx, color.CGColor);
  427. CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
  428. CGContextSetLineWidth(ctx, 3.0f);
  429. CGContextDrawPath(ctx, kCGPathFillStroke);
  430. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  431. UIGraphicsEndImageContext();
  432. return img;
  433. }
  434. #pragma mark - AVCapturePhotoCaptureDelegate
  435. -(void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error API_AVAILABLE(ios(11.0)) {
  436. if (!error) {
  437. // 使用该方式获取图片,可能图片会存在旋转问题,在使用的时候调整图片即可
  438. NSData *imageData = [photo fileDataRepresentation];
  439. UIImage *image = [UIImage imageWithData:imageData];
  440. // NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  441. // UIImage *image=[UIImage imageWithData:imageData];
  442. RATakePhotoPreviewController *preVC = [RATakePhotoPreviewController viewControllerFromStoryboard];
  443. preVC.photoHandler = ^(UIImage *img){
  444. if (self.completion) {
  445. self.completion(img);
  446. }
  447. };
  448. preVC.preImage = image;
  449. if (self.takeMode == RACameraTakeModeTakeOnce) {
  450. preVC.popTo = self.fromVC;
  451. preVC.barHidden = self.barHidden;
  452. }
  453. [self.navigationController pushViewController:preVC animated:YES];
  454. } else {
  455. NSLog(@"take picture failed: %@",error);
  456. }
  457. // sender.enabled = true;
  458. self.takePhotoBtn.enabled = true;
  459. }
  460. @end