TOCropViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //
  2. // TOCropViewController.h
  3. //
  4. // Copyright 2015 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOCropViewController.h"
  23. #import "TOCropView.h"
  24. #import "TOCropToolbar.h"
  25. #import "TOCropViewControllerTransitioning.h"
  26. #import "TOActivityCroppedImageProvider.h"
  27. #import "UIImage+CropRotate.h"
  28. #import "TOCroppedImageAttributes.h"
  29. typedef NS_ENUM(NSInteger, TOCropViewControllerAspectRatio) {
  30. TOCropViewControllerAspectRatioOriginal,
  31. TOCropViewControllerAspectRatioSquare,
  32. TOCropViewControllerAspectRatio3x2,
  33. TOCropViewControllerAspectRatio5x3,
  34. TOCropViewControllerAspectRatio4x3,
  35. TOCropViewControllerAspectRatio5x4,
  36. TOCropViewControllerAspectRatio7x5,
  37. TOCropViewControllerAspectRatio16x9
  38. };
  39. @interface TOCropViewController () <UIActionSheetDelegate, UIViewControllerTransitioningDelegate, TOCropViewDelegate>
  40. @property (nonatomic, readwrite) UIImage *image;
  41. @property (nonatomic, strong) TOCropToolbar *toolbar;
  42. @property (nonatomic, strong) TOCropView *cropView;
  43. @property (nonatomic, strong) UIView *snapshotView;
  44. @property (nonatomic, strong) TOCropViewControllerTransitioning *transitionController;
  45. @property (nonatomic, assign) BOOL inTransition;
  46. #pragma clang diagnostic push
  47. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  48. @property (nonatomic, strong) UIPopoverController *activityPopoverController;
  49. #pragma clang diagnostic pop
  50. /* Button callback */
  51. - (void)cancelButtonTapped;
  52. - (void)doneButtonTapped;
  53. - (void)showAspectRatioDialog;
  54. - (void)resetCropViewLayout;
  55. - (void)rotateCropView;
  56. /* View layout */
  57. - (CGRect)frameForToolBarWithVerticalLayout:(BOOL)verticalLayout;
  58. @end
  59. @implementation TOCropViewController
  60. - (instancetype)initWithImage:(UIImage *)image
  61. {
  62. self = [super init];
  63. if (self) {
  64. self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  65. self.modalPresentationStyle = UIModalPresentationFullScreen;
  66. _transitionController = [[TOCropViewControllerTransitioning alloc] init];
  67. _image = image;
  68. }
  69. return self;
  70. }
  71. - (void)viewDidLoad
  72. {
  73. [super viewDidLoad];
  74. BOOL landscapeLayout = CGRectGetWidth(self.view.frame) > CGRectGetHeight(self.view.frame);
  75. self.cropView = [[TOCropView alloc] initWithImage:self.image];
  76. self.cropView.frame = (CGRect){(landscapeLayout ? 44.0f : 0.0f),0,(CGRectGetWidth(self.view.bounds) - (landscapeLayout ? 44.0f : 0.0f)), (CGRectGetHeight(self.view.bounds)-(landscapeLayout ? 0.0f : 44.0f)) };
  77. self.cropView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  78. self.cropView.delegate = self;
  79. [self.view addSubview:self.cropView];
  80. self.toolbar = [[TOCropToolbar alloc] initWithFrame:CGRectZero];
  81. self.toolbar.frame = [self frameForToolBarWithVerticalLayout:CGRectGetWidth(self.view.bounds) < CGRectGetHeight(self.view.bounds)];
  82. [self.view addSubview:self.toolbar];
  83. __weak typeof(self) weakSelf = self;
  84. self.toolbar.doneButtonTapped = ^{ [weakSelf doneButtonTapped]; };
  85. self.toolbar.cancelButtonTapped = ^{ [weakSelf cancelButtonTapped]; };
  86. self.toolbar.resetButtonTapped = ^{ [weakSelf resetCropViewLayout]; };
  87. // self.toolbar.clampButtonTapped = ^{ [weakSelf showAspectRatioDialog]; };
  88. [self showAspectRatioDialogDS];
  89. self.toolbar.rotateButtonTapped = ^{ [weakSelf rotateCropView]; };
  90. self.transitioningDelegate = self;
  91. self.view.backgroundColor = self.cropView.backgroundColor;
  92. }
  93. - (void)viewWillAppear:(BOOL)animated
  94. {
  95. [super viewWillAppear:animated];
  96. if ([UIApplication sharedApplication].statusBarHidden == NO) {
  97. self.inTransition = YES;
  98. [self setNeedsStatusBarAppearanceUpdate];
  99. }
  100. }
  101. - (void)viewDidAppear:(BOOL)animated
  102. {
  103. [super viewDidAppear:animated];
  104. self.inTransition = NO;
  105. if (animated && [UIApplication sharedApplication].statusBarHidden == NO) {
  106. [UIView animateWithDuration:0.3f animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
  107. if (self.cropView.gridOverlayHidden)
  108. [self.cropView setGridOverlayHidden:NO animated:YES];
  109. }
  110. }
  111. - (void)viewWillDisappear:(BOOL)animated
  112. {
  113. [super viewWillDisappear:animated];
  114. self.inTransition = YES;
  115. [UIView animateWithDuration:0.5f animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
  116. }
  117. - (void)viewDidDisappear:(BOOL)animated
  118. {
  119. [super viewDidDisappear:animated];
  120. self.inTransition = NO;
  121. [self setNeedsStatusBarAppearanceUpdate];
  122. }
  123. #pragma mark - Status Bar -
  124. - (UIStatusBarStyle)preferredStatusBarStyle
  125. {
  126. return UIStatusBarStyleDefault;
  127. }
  128. - (BOOL)prefersStatusBarHidden
  129. {
  130. return !self.inTransition;
  131. }
  132. - (CGRect)frameForToolBarWithVerticalLayout:(BOOL)verticalLayout
  133. {
  134. CGRect frame = self.toolbar.frame;
  135. if (verticalLayout ) {
  136. frame = self.toolbar.frame;
  137. frame.origin.x = 0.0f;
  138. frame.origin.y = 0.0f;
  139. frame.size.width = 44.0f;
  140. frame.size.height = CGRectGetHeight(self.view.frame);
  141. }
  142. else {
  143. frame.origin.x = 0.0f;
  144. frame.origin.y = CGRectGetHeight(self.view.bounds) - 44.0f;
  145. frame.size.width = CGRectGetWidth(self.view.bounds);
  146. frame.size.height = 44.0f;
  147. }
  148. return frame;
  149. }
  150. - (void)viewDidLayoutSubviews
  151. {
  152. [super viewDidLayoutSubviews];
  153. BOOL verticalLayout = CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds);
  154. if (verticalLayout ) {
  155. CGRect frame = self.cropView.frame;
  156. frame.origin.x = 44.0f;
  157. frame.size.width = CGRectGetWidth(self.view.bounds) - 44.0f;
  158. frame.size.height = CGRectGetHeight(self.view.bounds);
  159. self.cropView.frame = frame;
  160. }
  161. else {
  162. CGRect frame = self.cropView.frame;
  163. frame.origin.x = 0.0f;
  164. frame.size.width = CGRectGetWidth(self.view.bounds);
  165. frame.size.height = CGRectGetHeight(self.view.bounds) - 44.0f;
  166. self.cropView.frame = frame;
  167. }
  168. [UIView setAnimationsEnabled:NO];
  169. self.toolbar.frame = [self frameForToolBarWithVerticalLayout:verticalLayout];
  170. [self.toolbar setNeedsLayout];
  171. [UIView setAnimationsEnabled:YES];
  172. }
  173. #pragma mark - Rotation Handling -
  174. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  175. {
  176. self.snapshotView = [self.toolbar snapshotViewAfterScreenUpdates:NO];
  177. self.snapshotView.frame = self.toolbar.frame;
  178. if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
  179. self.snapshotView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  180. else
  181. self.snapshotView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
  182. [self.view addSubview:self.snapshotView];
  183. self.toolbar.frame = [self frameForToolBarWithVerticalLayout:UIInterfaceOrientationIsLandscape(toInterfaceOrientation)];
  184. [self.toolbar layoutIfNeeded];
  185. self.toolbar.alpha = 0.0f;
  186. self.cropView.simpleMode = YES;
  187. [self.cropView prepareforRotation];
  188. }
  189. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  190. {
  191. self.toolbar.frame = [self frameForToolBarWithVerticalLayout:UIInterfaceOrientationIsLandscape(toInterfaceOrientation)];
  192. [UIView animateWithDuration:duration animations:^{
  193. self.snapshotView.alpha = 0.0f;
  194. self.toolbar.alpha = 1.0f;
  195. }];
  196. [self.cropView performRelayoutForRotation];
  197. }
  198. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  199. {
  200. [self.snapshotView removeFromSuperview];
  201. self.snapshotView = nil;
  202. [self.cropView setSimpleMode:NO animated:YES];
  203. }
  204. #pragma mark - Reset -
  205. - (void)resetCropViewLayout
  206. {
  207. [self.cropView resetLayoutToDefaultAnimated:YES];
  208. self.cropView.aspectLockEnabled = NO;
  209. self.toolbar.clampButtonGlowing = NO;
  210. [self showAspectRatioDialogDS];
  211. }
  212. #pragma mark - Aspect Ratio Handling -
  213. - (void)showAspectRatioDialogDS {
  214. if (self.cropView.aspectLockEnabled) {
  215. self.cropView.aspectLockEnabled = NO;
  216. self.toolbar.clampButtonGlowing = NO;
  217. return;
  218. }
  219. CGSize aspectRatio = CGSizeMake(4.0f, 3.0f);
  220. [self.cropView setAspectLockEnabledWithAspectRatio:aspectRatio animated:YES];
  221. self.toolbar.clampButtonGlowing = YES;
  222. }
  223. - (void)showAspectRatioDialog
  224. {
  225. if (self.cropView.aspectLockEnabled) {
  226. self.cropView.aspectLockEnabled = NO;
  227. self.toolbar.clampButtonGlowing = NO;
  228. return;
  229. }
  230. //TODO: Completely overhaul this once iOS 7 support is dropped
  231. #pragma clang diagnostic push
  232. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  233. // BOOL verticalCropBox = self.cropView.cropBoxAspectRatioIsPortrait;
  234. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
  235. delegate:self
  236. cancelButtonTitle:NSLocalizedStringFromTableInBundle(@"取消",
  237. @"TOCropViewControllerLocalizable",
  238. [NSBundle bundleForClass:[self class]],
  239. nil)
  240. destructiveButtonTitle:nil
  241. otherButtonTitles:NSLocalizedStringFromTableInBundle(@"原始",
  242. @"TOCropViewControllerLocalizable",
  243. [NSBundle bundleForClass:[self class]],
  244. nil),
  245. NSLocalizedStringFromTableInBundle(@"正方形",
  246. @"TOCropViewControllerLocalizable",
  247. [NSBundle bundleForClass:[self class]],
  248. nil),
  249. @"3:2",
  250. @"4:3",
  251. @"16:9",nil];
  252. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  253. [actionSheet showFromRect:self.toolbar.clampButtonFrame inView:self.toolbar animated:YES];
  254. else
  255. [actionSheet showInView:self.view];
  256. #pragma clang diagnostic pop
  257. }
  258. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
  259. {
  260. CGSize aspectRatio = CGSizeZero;
  261. switch (buttonIndex) {
  262. case TOCropViewControllerAspectRatioOriginal:
  263. aspectRatio = CGSizeZero;
  264. break;
  265. case TOCropViewControllerAspectRatioSquare:
  266. aspectRatio = CGSizeMake(1.0f, 1.0f);
  267. break;
  268. case TOCropViewControllerAspectRatio3x2:
  269. aspectRatio = CGSizeMake(3.0f, 2.0f);
  270. break;
  271. case TOCropViewControllerAspectRatio5x3:
  272. aspectRatio = CGSizeMake(5.0f, 3.0f);
  273. break;
  274. case TOCropViewControllerAspectRatio4x3:
  275. aspectRatio = CGSizeMake(4.0f, 3.0f);
  276. break;
  277. case TOCropViewControllerAspectRatio5x4:
  278. aspectRatio = CGSizeMake(5.0f, 4.0f);
  279. break;
  280. case TOCropViewControllerAspectRatio7x5:
  281. aspectRatio = CGSizeMake(7.0f, 5.0f);
  282. break;
  283. case TOCropViewControllerAspectRatio16x9:
  284. aspectRatio = CGSizeMake(16.0f, 9.0f);
  285. break;
  286. default:
  287. return;
  288. }
  289. // if (self.cropView.cropBoxAspectRatioIsPortrait) {
  290. // CGFloat width = aspectRatio.width;
  291. // aspectRatio.width = aspectRatio.height;
  292. // aspectRatio.height = width;
  293. // }
  294. [self.cropView setAspectLockEnabledWithAspectRatio:aspectRatio animated:YES];
  295. self.toolbar.clampButtonGlowing = YES;
  296. }
  297. - (void)rotateCropView
  298. {
  299. [self.cropView rotateImageNinetyDegreesAnimated:YES];
  300. }
  301. #pragma mark - Crop View Delegates -
  302. - (void)cropViewDidBecomeResettable:(TOCropView *)cropView
  303. {
  304. self.toolbar.resetButtonEnabled = YES;
  305. }
  306. - (void)cropViewDidBecomeNonResettable:(TOCropView *)cropView
  307. {
  308. self.toolbar.resetButtonEnabled = NO;
  309. }
  310. #pragma mark - Presentation Handling -
  311. - (void)presentAnimatedFromParentViewController:(UIViewController *)viewController fromFrame:(CGRect)frame completion:(void (^)(void))completion
  312. {
  313. self.transitionController.image = self.image;
  314. self.transitionController.fromFrame = frame;
  315. __weak typeof (self) weakSelf = self;
  316. [viewController presentViewController:self animated:YES completion:^ {
  317. typeof (self) strongSelf = weakSelf;
  318. if (completion) {
  319. completion();
  320. }
  321. [strongSelf.cropView setCroppingViewsHidden:NO animated:YES];
  322. if (!CGRectIsEmpty(frame)) {
  323. [strongSelf.cropView setGridOverlayHidden:NO animated:YES];
  324. }
  325. }];
  326. }
  327. - (void)dismissAnimatedFromParentViewController:(UIViewController *)viewController withCroppedImage:(UIImage *)image toFrame:(CGRect)frame completion:(void (^)(void))completion
  328. {
  329. self.transitionController.image = image;
  330. self.transitionController.fromFrame = [self.cropView convertRect:self.cropView.cropBoxFrame toView:self.view];
  331. self.transitionController.toFrame = frame;
  332. [viewController dismissViewControllerAnimated:YES completion:^ {
  333. if (completion) {
  334. completion();
  335. }
  336. }];
  337. }
  338. - (void)dismissAnimatedFromParentViewController:(UIViewController *)viewController toFrame:(CGRect)frame completion:(void (^)(void))completion
  339. {
  340. self.transitionController.image = self.image;
  341. self.transitionController.fromFrame = [self.cropView convertRect:self.cropView.imageViewFrame toView:self.view];
  342. self.transitionController.toFrame = frame;
  343. [viewController dismissViewControllerAnimated:YES completion:^ {
  344. if (completion) {
  345. completion();
  346. }
  347. }];
  348. }
  349. - (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
  350. {
  351. __weak typeof (self) weakSelf = self;
  352. self.transitionController.prepareForTransitionHandler = ^{
  353. typeof (self) strongSelf = weakSelf;
  354. strongSelf.transitionController.toFrame = [strongSelf.cropView convertRect:strongSelf.cropView.cropBoxFrame toView:strongSelf.view];
  355. if (!CGRectIsEmpty(strongSelf.transitionController.fromFrame))
  356. strongSelf.cropView.croppingViewsHidden = YES;
  357. if (strongSelf.prepareForTransitionHandler)
  358. strongSelf.prepareForTransitionHandler();
  359. strongSelf.prepareForTransitionHandler = nil;
  360. };
  361. self.transitionController.isDismissing = NO;
  362. return self.transitionController;
  363. }
  364. - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
  365. {
  366. __weak typeof (self) weakSelf = self;
  367. self.transitionController.prepareForTransitionHandler = ^{
  368. typeof (self) strongSelf = weakSelf;
  369. if (!CGRectIsEmpty(strongSelf.transitionController.toFrame))
  370. strongSelf.cropView.croppingViewsHidden = YES;
  371. else
  372. strongSelf.cropView.simpleMode = YES;
  373. if (strongSelf.prepareForTransitionHandler)
  374. strongSelf.prepareForTransitionHandler();
  375. };
  376. self.transitionController.isDismissing = YES;
  377. return self.transitionController;
  378. }
  379. #pragma mark - Button Feedback -
  380. - (void)cancelButtonTapped
  381. {
  382. if ([self.delegate respondsToSelector:@selector(cropViewController:didFinishCancelled:)]) {
  383. [self.delegate cropViewController:self didFinishCancelled:YES];
  384. return;
  385. }
  386. self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  387. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  388. }
  389. - (void)doneButtonTapped
  390. {
  391. CGRect cropFrame = self.cropView.croppedImageFrame;
  392. NSInteger angle = self.cropView.angle;
  393. //If desired, when the user taps done, show an activity sheet
  394. if (self.showActivitySheetOnDone) {
  395. TOActivityCroppedImageProvider *imageItem = [[TOActivityCroppedImageProvider alloc] initWithImage:self.image cropFrame:cropFrame angle:angle];
  396. TOCroppedImageAttributes *attributes = [[TOCroppedImageAttributes alloc] initWithCroppedFrame:cropFrame angle:angle originalImageSize:self.image.size];
  397. NSMutableArray *activityItems = [@[imageItem, attributes] mutableCopy];
  398. if (self.activityItems)
  399. [activityItems addObjectsFromArray:self.activityItems];
  400. UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:self.applicationActivities];
  401. activityController.excludedActivityTypes = self.excludedActivityTypes;
  402. if (NSClassFromString(@"UIPopoverPresentationController")) {
  403. activityController.modalPresentationStyle = UIModalPresentationPopover;
  404. activityController.popoverPresentationController.sourceView = self.toolbar;
  405. activityController.popoverPresentationController.sourceRect = self.toolbar.doneButtonFrame;
  406. [self presentViewController:activityController animated:YES completion:nil];
  407. }
  408. else {
  409. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  410. [self presentViewController:activityController animated:YES completion:nil];
  411. }
  412. else {
  413. #pragma clang diagnostic push
  414. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  415. [self.activityPopoverController dismissPopoverAnimated:NO];
  416. self.activityPopoverController = [[UIPopoverController alloc] initWithContentViewController:activityController];
  417. [self.activityPopoverController presentPopoverFromRect:self.toolbar.doneButtonFrame inView:self.toolbar permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  418. #pragma clang diagnostic pop
  419. }
  420. }
  421. __weak typeof(activityController) blockController = activityController;
  422. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
  423. activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  424. if (!completed)
  425. return;
  426. if ([self.delegate respondsToSelector:@selector(cropViewController:didFinishCancelled:)]) {
  427. [self.delegate cropViewController:self didFinishCancelled:NO];
  428. }
  429. else {
  430. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  431. blockController.completionWithItemsHandler = nil;
  432. }
  433. };
  434. #else
  435. activityController.completionHandler = ^(NSString *activityType, BOOL completed) {
  436. if (!completed)
  437. return;
  438. if ([self.delegate respondsToSelector:@selector(cropViewController:didFinishCancelled:)]) {
  439. [self.delegate cropViewController:self didFinishCancelled:NO];
  440. }
  441. else {
  442. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  443. blockController.completionHandler = nil;
  444. }
  445. };
  446. #endif
  447. return;
  448. }
  449. //If the delegate that only supplies crop data is provided, call it
  450. if ([self.delegate respondsToSelector:@selector(cropViewController:didCropImageToRect:angle:)]) {
  451. [self.delegate cropViewController:self didCropImageToRect:cropFrame angle:angle];
  452. }
  453. //If the delegate that requires the specific cropped image is provided, call it
  454. else if ([self.delegate respondsToSelector:@selector(cropViewController:didCropToImage:withRect:angle:)]) {
  455. UIImage *image = nil;
  456. if (angle == 0 && CGRectEqualToRect(cropFrame, (CGRect){CGPointZero, self.image.size})) {
  457. image = self.image;
  458. }
  459. else {
  460. image = [self.image croppedImageWithFrame:cropFrame angle:angle];
  461. }
  462. //dispatch on the next run-loop so the animation isn't interuppted by the crop operation
  463. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.03f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  464. [self.delegate cropViewController:self didCropToImage:image withRect:cropFrame angle:angle];
  465. });
  466. }
  467. else {
  468. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  469. }
  470. }
  471. @end