HX_PhotoEditViewController.m 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. //
  2. // HX_PhotoEditViewController.m
  3. // photoEditDemo
  4. //
  5. // Created by Silence on 2020/6/20.
  6. // Copyright © 2020 Silence. All rights reserved.
  7. //
  8. #import "HX_PhotoEditViewController.h"
  9. #import "HX_PhotoEditBottomView.h"
  10. #import "UIView+HXExtension.h"
  11. #import "HXPhotoEditGraffitiColorView.h"
  12. #import "HXPhotoEditMosaicView.h"
  13. #import "HXPhotoEditChartletListView.h"
  14. #import "HXPhotoEditTextView.h"
  15. #import "HXPhotoDefine.h"
  16. #import "HXPhotoEditImageView.h"
  17. #import "HXPhotoEditStickerItem.h"
  18. #import "HXPhotoEditingView.h"
  19. #import "HXPhotoClippingView.h"
  20. #import "HXPhotoEditClippingToolBar.h"
  21. #import "UIImage+HXExtension.h"
  22. #import "UIButton+HXExtension.h"
  23. #import "HXMECancelBlock.h"
  24. #import "UIColor+HXExtension.h"
  25. #import "HXPhotoModel.h"
  26. #import "HXPhotoEditTransition.h"
  27. #import "HXPhotoTools.h"
  28. #import "HXPhotoEditGraffitiColorSizeView.h"
  29. #define HXGraffitiColorViewHeight 60.f
  30. #define HXmosaicViewHeight 60.f
  31. #define HXClippingToolBar 110.f
  32. @interface HX_PhotoEditViewController ()<UIGestureRecognizerDelegate, HXPhotoEditingViewDelegate>
  33. @property (strong, nonatomic) UIView *topMaskView;
  34. @property (strong, nonatomic) CAGradientLayer *topMaskLayer;
  35. @property (strong, nonatomic) UIView *bottomMaskView;
  36. @property (strong, nonatomic) CAGradientLayer *bottomMaskLayer;
  37. @property (strong, nonatomic) HX_PhotoEditBottomView *toolsView;
  38. @property (strong, nonatomic) HXPhotoEditGraffitiColorView *graffitiColorView;
  39. @property (strong, nonatomic) HXPhotoEditMosaicView *mosaicView;
  40. @property (strong, nonatomic) UIButton *backBtn;
  41. @property (strong, nonatomic) HXPhotoEditingView *editingView;
  42. @property (weak, nonatomic) UITapGestureRecognizer *tap;
  43. @property (strong, nonatomic) HXPhotoEditClippingToolBar *clippingToolBar;
  44. @property (nonatomic, strong, nullable) NSDictionary *editData;
  45. @property (assign, nonatomic) PHContentEditingInputRequestID requestId;
  46. @property (assign, nonatomic) CGFloat imageWidth;
  47. @property (assign, nonatomic) CGFloat imageHeight;
  48. @property (strong, nonatomic) HXPhotoModel *afterModel;
  49. @property (assign, nonatomic) BOOL orientationDidChange;
  50. @property (strong, nonatomic) HXPhotoEditGraffitiColorSizeView *graffitiColorSizeView;
  51. @property (strong, nonatomic) UIView *brushLineWidthPromptView;
  52. @end
  53. @implementation HX_PhotoEditViewController
  54. - (void)dealloc {
  55. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  56. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
  57. }
  58. - (instancetype)init {
  59. self = [super init];
  60. if (self) {
  61. self.transitioningDelegate = self;
  62. self.modalPresentationStyle = UIModalPresentationCustom;
  63. self.isAutoBack = YES;
  64. }
  65. return self;
  66. }
  67. - (instancetype)initWithConfiguration:(HXPhotoEditConfiguration *)configuration {
  68. self = [super init];
  69. if (self) {
  70. self.transitioningDelegate = self;
  71. self.modalPresentationStyle = UIModalPresentationCustom;
  72. self.configuration = configuration;
  73. self.isAutoBack = YES;
  74. }
  75. return self;
  76. }
  77. - (instancetype)initWithPhotoEdit:(HXPhotoEdit *)photoEdit
  78. configuration:(HXPhotoEditConfiguration *)configuration {
  79. self = [super init];
  80. if (self) {
  81. self.transitioningDelegate = self;
  82. self.modalPresentationStyle = UIModalPresentationCustom;
  83. self.photoEdit = photoEdit;
  84. self.configuration = configuration;
  85. self.isAutoBack = YES;
  86. }
  87. return self;
  88. }
  89. - (instancetype)initWithEditImage:(UIImage *)editImage
  90. configuration:(HXPhotoEditConfiguration *)configuration {
  91. self = [super init];
  92. if (self) {
  93. self.transitioningDelegate = self;
  94. self.modalPresentationStyle = UIModalPresentationCustom;
  95. self.editImage = editImage;
  96. self.configuration = configuration;
  97. self.isAutoBack = YES;
  98. }
  99. return self;
  100. }
  101. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
  102. return [HXPhotoEditTransition transitionWithType:HXPhotoEditTransitionTypePresent model:self.photoModel];
  103. }
  104. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  105. if (!self.isAutoBack && !self.isCancel) {
  106. return nil;
  107. }
  108. return [HXPhotoEditTransition transitionWithType:HXPhotoEditTransitionTypeDismiss model:self.photoModel];
  109. }
  110. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
  111. return UIRectEdgeAll;
  112. }
  113. - (BOOL)shouldAutorotate {
  114. if (!self.supportRotation) {
  115. return NO;
  116. }
  117. if (self.configuration.supportRotation) {
  118. return YES;
  119. }
  120. return NO;
  121. }
  122. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  123. if (!self.supportRotation) {
  124. return self.configuration.supportedInterfaceOrientations;
  125. }
  126. if (self.configuration.supportRotation) {
  127. return UIInterfaceOrientationMaskAll;
  128. }else {
  129. return UIInterfaceOrientationMaskPortrait;
  130. }
  131. }
  132. - (void)viewWillDisappear:(BOOL)animated {
  133. [super viewWillDisappear:animated];
  134. if (self.requestId) {
  135. [self.photoModel.asset cancelContentEditingInputRequest:self.requestId];
  136. }
  137. }
  138. - (void)viewWillAppear:(BOOL)animated {
  139. [super viewWillAppear:animated];
  140. [self.navigationController setNavigationBarHidden:YES];
  141. #pragma clang diagnostic push
  142. #pragma clang diagnostic ignored"-Wdeprecated-declarations"
  143. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
  144. #pragma clang diagnostic pop
  145. }
  146. - (BOOL)prefersStatusBarHidden {
  147. return YES;
  148. }
  149. - (void)viewDidLayoutSubviews {
  150. [super viewDidLayoutSubviews];
  151. if (self.orientationDidChange) {
  152. [self changeSubviewFrame];
  153. self.orientationDidChange = NO;
  154. }
  155. }
  156. - (void)viewDidLoad {
  157. [super viewDidLoad];
  158. // Do any additional setup after loading the view.
  159. self.orientationDidChange = NO;
  160. [self setupUI];
  161. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  162. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationWillChanged:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
  163. [self hiddenTopBottomView];
  164. }
  165. - (void)setupUI {
  166. if (!self.onlyCliping) {
  167. self.onlyCliping = self.configuration.onlyCliping;
  168. }
  169. self.backBtn.alpha = 0;
  170. self.clippingToolBar.alpha = 0;
  171. self.toolsView.alpha = 0;
  172. self.topMaskView.alpha = 0;
  173. self.bottomMaskView.alpha = 0;
  174. self.view.backgroundColor = [UIColor blackColor];
  175. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didBgViewClick)];
  176. tap.numberOfTapsRequired = 1;
  177. tap.delegate = self;
  178. [self.view addGestureRecognizer:tap];
  179. self.tap = tap;
  180. self.view.exclusiveTouch = YES;
  181. [self setupPhotoModel];
  182. [self.view addSubview:self.editingView];
  183. [self.view addSubview:self.bottomMaskView];
  184. if (self.onlyCliping) {
  185. [self.view addSubview:self.clippingToolBar];
  186. [self.editingView photoEditEnable:NO];
  187. self.tap.enabled = NO;
  188. self.clippingToolBar.userInteractionEnabled = YES;
  189. }else {
  190. [self.view addSubview:self.topMaskView];
  191. [self.topMaskView addSubview:self.backBtn];
  192. [self.view addSubview:self.toolsView];
  193. [self.view addSubview:self.clippingToolBar];
  194. [self.view addSubview:self.brushLineWidthPromptView];
  195. }
  196. }
  197. - (void)deviceOrientationWillChanged:(NSNotification *)notify {
  198. if (self.editingView.clipping) {
  199. [self.toolsView endCliping];
  200. [self.editingView cancelClipping:NO];
  201. }
  202. [self.editingView resetRotateAngle];
  203. self.editingView.zoomScale = 1;
  204. self.editingView.clippingView.zoomScale = 1;
  205. }
  206. - (void)deviceOrientationChanged:(NSNotification *)notify {
  207. self.orientationDidChange = YES;
  208. }
  209. - (void)changeSubviewFrame {
  210. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  211. CGFloat leftMargin = hxBottomMargin;
  212. if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown || HX_UI_IS_IPAD) {
  213. leftMargin = 0;
  214. self.backBtn.hx_x = 20;
  215. self.backBtn.hx_y = hxNavigationBarHeight - 20 - self.backBtn.hx_h;
  216. self.clippingToolBar.frame = CGRectMake(0, self.view.hx_h - HXClippingToolBar - hxBottomMargin, self.view.hx_w, HXClippingToolBar + hxBottomMargin);
  217. self.toolsView.frame = CGRectMake(0, self.view.hx_h - 50 - hxBottomMargin, self.view.hx_w, 50 + hxBottomMargin);
  218. }else if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
  219. self.backBtn.hx_x = 20 + hxBottomMargin;
  220. self.backBtn.hx_y = 20;
  221. self.clippingToolBar.frame = CGRectMake(hxBottomMargin, self.view.hx_h - HXClippingToolBar, self.view.hx_w - hxBottomMargin * 2, HXClippingToolBar);
  222. self.toolsView.frame = CGRectMake(hxBottomMargin, self.view.hx_h - 50, self.view.hx_w - hxBottomMargin * 2, 50);
  223. }
  224. self.graffitiColorView.frame = CGRectMake(leftMargin, self.toolsView.hx_y - HXGraffitiColorViewHeight, self.view.hx_w - leftMargin * 2, HXGraffitiColorViewHeight);
  225. self.graffitiColorSizeView.frame = CGRectMake(self.view.hx_w - 50 - 12, 0, 50, 180);
  226. self.graffitiColorSizeView.hx_centerY = self.view.hx_h / 2;
  227. [self setBrushinePromptViewSize];
  228. self.mosaicView.frame = CGRectMake(leftMargin, self.toolsView.hx_y - HXmosaicViewHeight, self.view.hx_w - leftMargin * 2, HXmosaicViewHeight);
  229. self.topMaskView.frame = CGRectMake(0, 0, HX_ScreenWidth, hxNavigationBarHeight);
  230. self.topMaskLayer.frame = CGRectMake(0, 0, HX_ScreenWidth, hxNavigationBarHeight + 30.f);
  231. self.bottomMaskView.frame = CGRectMake(0, HX_ScreenHeight - hxBottomMargin - 120, HX_ScreenWidth, hxBottomMargin + 120);
  232. self.bottomMaskLayer.frame = self.bottomMaskView.bounds;
  233. if (self.orientationDidChange) {
  234. self.editingView.frame = self.view.bounds;
  235. [self.editingView changeSubviewFrame];
  236. self.editingView.image = self.editImage;
  237. [self.editingView clearCoverage];
  238. }
  239. }
  240. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  241. if ([touch.view isKindOfClass:NSClassFromString(@"HXPhotoEditStickerItemContentView")] ||
  242. touch.view == self.backBtn) {
  243. return NO;
  244. }
  245. if ([touch.view isDescendantOfView:self.editingView] ||
  246. touch.view == self.view ||
  247. touch.view == self.topMaskView) {
  248. return YES;
  249. }
  250. return NO;
  251. }
  252. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  253. return YES;
  254. }
  255. - (void)setupPhotoModel {
  256. self.imageWidth = self.photoModel.imageSize.width;
  257. self.imageHeight = self.photoModel.imageSize.height;
  258. if (self.photoModel.photoEdit) {
  259. self.imageRequestComplete = YES;
  260. if (!self.transitionCompletion) {
  261. self.editingView.hidden = YES;
  262. }
  263. self.photoEdit = self.photoModel.photoEdit;
  264. if (self.editImage) {
  265. self.editingView.image = self.editImage;
  266. [self setupPhotoData];
  267. }else {
  268. [self setAsetImage];
  269. }
  270. }else {
  271. [self setAsetImage];
  272. }
  273. }
  274. - (void)setAsetImage {
  275. if (self.photoModel.asset ||
  276. self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWork ||
  277. self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWorkGif ||
  278. self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeLocalLivePhoto ||
  279. self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWorkLivePhoto) {
  280. [self requestImageData];
  281. }else {
  282. UIImage *image;
  283. if (self.photoModel.thumbPhoto.images.count > 1) {
  284. image = self.photoModel.thumbPhoto.images.firstObject;
  285. }else {
  286. image = self.photoModel.thumbPhoto;
  287. }
  288. CGSize imageSize = image.size;
  289. if (imageSize.width * imageSize.height > 3 * 1000 * 1000) {
  290. while (imageSize.width * imageSize.height > 3 * 1000 * 1000) {
  291. imageSize.width /= 2;
  292. imageSize.height /= 2;
  293. }
  294. image = [image hx_scaleToFillSize:imageSize];
  295. }
  296. self.editImage = image;
  297. [self loadImageCompletion];
  298. }
  299. }
  300. - (void)setupPhotoData {
  301. if (self.editData) {
  302. self.editingView.photoEditData = self.editData;
  303. self.graffitiColorView.undo = self.editingView.clippingView.imageView.drawView.canUndo;
  304. self.mosaicView.undo = self.editingView.clippingView.imageView.splashView.canUndo;
  305. self.editData = nil;
  306. }
  307. }
  308. - (void)requestImaegURL {
  309. HXWeakSelf
  310. self.requestId = [self.photoModel requestImageURLStartRequestICloud:^(PHContentEditingInputRequestID iCloudRequestId, HXPhotoModel *model) {
  311. weakSelf.requestId = iCloudRequestId;
  312. } progressHandler:nil success:^(NSURL *imageURL, HXPhotoModel *model, NSDictionary *info) {
  313. @autoreleasepool {
  314. NSData * imageData = [NSData dataWithContentsOfFile:imageURL.relativePath];
  315. UIImage *image = [UIImage imageWithData:imageData];
  316. [weakSelf requestImageCompletion:image];
  317. }
  318. } failed:^(NSDictionary *info, HXPhotoModel *model) {
  319. [weakSelf requestImage];
  320. }];
  321. }
  322. - (void)requestImageData {
  323. HXWeakSelf
  324. if (self.photoModel.type == HXPhotoModelMediaTypeLivePhoto) {
  325. [self.photoModel requestPreviewImageWithSize:self.photoModel.endImageSize startRequestICloud:^(PHImageRequestID iCloudRequestId, HXPhotoModel * _Nullable model) {
  326. weakSelf.requestId = iCloudRequestId;
  327. } progressHandler:nil success:^(UIImage * _Nullable image, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
  328. weakSelf.editImage = image;
  329. [weakSelf.view hx_handleLoading];
  330. [weakSelf loadImageCompletion];
  331. } failed:^(NSDictionary * _Nullable info, HXPhotoModel * _Nullable model) {
  332. [weakSelf.view hx_handleLoading];
  333. [weakSelf loadImageCompletion];
  334. }];
  335. return;
  336. }else if (self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeLocalLivePhoto ||
  337. self.photoModel.cameraPhotoType == HXPhotoModelMediaTypeCameraPhotoTypeNetWorkLivePhoto) {
  338. self.editImage = self.photoModel.thumbPhoto;
  339. [self.view hx_handleLoading];
  340. [self loadImageCompletion];
  341. return;
  342. }
  343. self.requestId = [self.photoModel requestImageDataWithLoadOriginalImage:YES startRequestICloud:^(PHImageRequestID iCloudRequestId, HXPhotoModel * _Nullable model) {
  344. weakSelf.requestId = iCloudRequestId;
  345. } progressHandler:nil success:^(NSData * _Nullable imageData, UIImageOrientation orientation, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
  346. @autoreleasepool {
  347. UIImage *image = [UIImage imageWithData:imageData];
  348. [weakSelf requestImageCompletion:image];
  349. }
  350. } failed:^(NSDictionary * _Nullable info, HXPhotoModel * _Nullable model) {
  351. [weakSelf requestImaegURL];
  352. }];
  353. }
  354. - (void)requestImage {
  355. HXWeakSelf
  356. self.requestId = [self.photoModel requestPreviewImageWithSize:PHImageManagerMaximumSize startRequestICloud:^(PHImageRequestID iCloudRequestId, HXPhotoModel * _Nullable model) {
  357. weakSelf.requestId = iCloudRequestId;
  358. } progressHandler:nil success:^(UIImage * _Nullable image, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
  359. if (image.images.count > 1) {
  360. image = image.images.firstObject;
  361. }
  362. [weakSelf requestImageCompletion:image];
  363. } failed:^(NSDictionary * _Nullable info, HXPhotoModel * _Nullable model) {
  364. [weakSelf.view hx_handleLoading];
  365. [weakSelf loadImageCompletion];
  366. }];
  367. }
  368. - (void)requestImageCompletion:(UIImage *)image {
  369. if (image.imageOrientation != UIImageOrientationUp) {
  370. image = [image hx_normalizedImage];
  371. }
  372. CGSize imageSize = image.size;
  373. if (imageSize.width * imageSize.height > 3 * 1000 * 1000) {
  374. while (imageSize.width * imageSize.height > 3 * 1000 * 1000) {
  375. imageSize.width /= 2;
  376. imageSize.height /= 2;
  377. }
  378. image = [image hx_scaleToFillSize:imageSize];
  379. }
  380. self.editImage = image;
  381. [self loadImageCompletion];
  382. }
  383. - (void)loadImageCompletion {
  384. self.imageRequestComplete = YES;
  385. if (self.transitionCompletion) {
  386. self.editingView.image = self.editImage;
  387. [self setupPhotoData];
  388. if (self.onlyCliping) {
  389. [self.editingView setClipping:YES animated:YES];
  390. [self.clippingToolBar setRotateAlpha:1.f];
  391. [UIView animateWithDuration:0.2 animations:^{
  392. self.clippingToolBar.alpha = 1;
  393. }];
  394. }
  395. [self.view hx_handleLoading];
  396. }
  397. }
  398. - (CGRect)getImageFrame {
  399. CGFloat width = self.view.hx_w;
  400. CGFloat height = self.view.hx_h;
  401. CGFloat imgWidth = self.imageWidth;
  402. CGFloat imgHeight = self.imageHeight;
  403. imgHeight = width / imgWidth * imgHeight;
  404. CGFloat y = 0;
  405. if (imgHeight < height) {
  406. y = (height - imgHeight) / 2;
  407. }
  408. return CGRectMake(0, y, width, imgHeight);
  409. }
  410. - (CGRect)getDismissImageFrame {
  411. CGFloat screenScale = self.editingView.zoomScale;
  412. CGFloat width = self.editingView.clippingView.hx_w * screenScale;
  413. CGFloat height = self.editingView.clippingView.hx_h * screenScale;
  414. CGFloat imageX = (self.editingView.clipZoomView.hx_w / screenScale - self.editingView.clippingView.hx_w) / 2 * screenScale;
  415. CGFloat imageY = (self.editingView.clipZoomView.hx_h / screenScale - self.editingView.clippingView.hx_h) / 2 * screenScale;
  416. CGRect rect = [self.editingView convertRect:CGRectMake(imageX, imageY, width, height) toView:[UIApplication sharedApplication].keyWindow];
  417. return rect;
  418. }
  419. - (UIImage *)getCurrentImage {
  420. if (self.photoModel.photoEdit) {
  421. return self.photoModel.photoEdit.editPreviewImage;
  422. }
  423. return self.editImage;
  424. }
  425. - (void)hideImageView {
  426. self.editingView.hidden = YES;
  427. }
  428. - (void)completeTransition:(UIImage *)image {
  429. self.transitionCompletion = YES;
  430. self.editingView.hidden = NO;
  431. if (self.photoModel.photoEdit) {
  432. if (!self.editingView.image) {
  433. self.editingView.image = self.editImage;
  434. [self setupPhotoData];
  435. }
  436. self.editingView.hidden = NO;
  437. if (self.onlyCliping) {
  438. [self.editingView setClipping:YES animated:YES];
  439. [self.clippingToolBar setRotateAlpha:1.f];
  440. [UIView animateWithDuration:0.2 animations:^{
  441. self.clippingToolBar.alpha = 1;
  442. }];
  443. }
  444. return;
  445. }
  446. if (self.imageRequestComplete) {
  447. [self loadImageCompletion];
  448. }else {
  449. [self.view hx_showLoadingHUDText:nil];
  450. self.editingView.image = image;
  451. }
  452. }
  453. - (void)hiddenTopBottomView {
  454. self.backBtn.hx_y = hxNavigationBarHeight - 20 - self.backBtn.hx_h - 15;
  455. self.clippingToolBar.hx_y = self.view.hx_h;
  456. self.toolsView.hx_y = self.view.hx_h;
  457. // self.topMaskView.hx_y = -hxNavigationBarHeight;
  458. // self.bottomMaskView.hx_y = self.view.hx_h;
  459. }
  460. - (void)showTopBottomView {
  461. [self changeSubviewFrame];
  462. self.backBtn.alpha = 1;
  463. if (self.onlyCliping) {
  464. self.clippingToolBar.alpha = 1;
  465. }
  466. self.toolsView.alpha = 1;
  467. self.topMaskView.alpha = 1;
  468. self.bottomMaskView.alpha = 1;
  469. }
  470. - (void)showBgViews {
  471. self.backBtn.userInteractionEnabled = YES;
  472. self.toolsView.userInteractionEnabled = YES;
  473. self.graffitiColorView.userInteractionEnabled = YES;
  474. self.graffitiColorSizeView.userInteractionEnabled = YES;
  475. self.mosaicView.userInteractionEnabled = YES;
  476. [UIView animateWithDuration:0.25 animations:^{
  477. self.backBtn.alpha = 1;
  478. self.toolsView.alpha = 1;
  479. self.graffitiColorView.alpha = 1;
  480. self.graffitiColorSizeView.alpha = 1;
  481. self.mosaicView.alpha = 1;
  482. self.topMaskView.alpha = 1;
  483. self.bottomMaskView.alpha = 1;
  484. }];
  485. }
  486. - (void)hideBgViews {
  487. self.backBtn.userInteractionEnabled = NO;
  488. self.toolsView.userInteractionEnabled = NO;
  489. self.graffitiColorView.userInteractionEnabled = NO;
  490. self.graffitiColorSizeView.userInteractionEnabled = NO;
  491. self.mosaicView.userInteractionEnabled = NO;
  492. [UIView animateWithDuration:0.25 animations:^{
  493. self.backBtn.alpha = 0;
  494. self.toolsView.alpha = 0;
  495. self.graffitiColorView.alpha = 0;
  496. self.graffitiColorSizeView.alpha = 0;
  497. self.mosaicView.alpha = 0;
  498. self.topMaskView.alpha = 0;
  499. self.bottomMaskView.alpha = 0;
  500. }];
  501. [self hiddenBrushLineWidthPromptView];
  502. }
  503. - (void)didBgViewClick {
  504. [UIView cancelPreviousPerformRequestsWithTarget:self];
  505. if (self.toolsView.alpha != 1) {
  506. [self showBgViews];
  507. }else {
  508. [self hideBgViews];
  509. }
  510. }
  511. #pragma mark - HXPhotoEditingViewDelegate
  512. - (void)editingViewViewDidEndZooming:(HXPhotoEditingView *)editingView {
  513. CGFloat maxWidth = self.configuration.brushLineMaxWidth;
  514. CGFloat minWidth = self.configuration.brushLineMinWidth;
  515. CGFloat lineWidth = minWidth + (maxWidth - minWidth) * self.graffitiColorSizeView.scale / editingView.zoomScale;
  516. self.editingView.drawLineWidth = lineWidth;
  517. }
  518. /** 开始编辑目标 */
  519. - (void)editingViewWillBeginEditing:(HXPhotoEditingView *)EditingView {
  520. BOOL aspectRotioNone = self.configuration.aspectRatio == HXPhotoEditAspectRatioType_None;
  521. [UIView animateWithDuration:0.25f animations:^{
  522. [self.clippingToolBar setRotateAlpha: aspectRotioNone ? 0.5f : 0.f];
  523. }];
  524. }
  525. /** 停止编辑目标 */
  526. - (void)editingViewDidEndEditing:(HXPhotoEditingView *)EditingView {
  527. [UIView animateWithDuration:0.25f animations:^{
  528. [self.clippingToolBar setRotateAlpha:1.f];
  529. }];
  530. self.clippingToolBar.enableReset = self.editingView.canReset;
  531. }
  532. /** 进入剪切界面 */
  533. - (void)editingViewDidAppearClip:(HXPhotoEditingView *)EditingView {
  534. self.clippingToolBar.enableReset = self.editingView.canReset;
  535. }
  536. /// 离开剪切界面
  537. - (void)editingViewDidDisappearClip:(HXPhotoEditingView *)EditingView {
  538. }
  539. - (void)setEditImage:(UIImage *)editImage {
  540. // if (!self.photoEdit) {
  541. _editImage = HX_UIImageDecodedCopy(editImage);
  542. // }
  543. }
  544. - (void)setPhotoEdit:(HXPhotoEdit *)photoEdit {
  545. _photoEdit = photoEdit;
  546. if (photoEdit) {
  547. NSData *imageData = [NSData dataWithContentsOfFile:photoEdit.imagePath];
  548. _editImage = [UIImage imageWithData:imageData];
  549. self.editData = photoEdit.editData;
  550. }
  551. }
  552. - (UIButton *)backBtn {
  553. if (!_backBtn) {
  554. _backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  555. [_backBtn setImage:[UIImage hx_imageContentsOfFile:@"hx_photo_edit_back"] forState:UIControlStateNormal];
  556. _backBtn.imageView.tintColor = [UIColor whiteColor];
  557. _backBtn.tintColor = [UIColor whiteColor];
  558. _backBtn.hx_size = _backBtn.currentImage.size;
  559. _backBtn.hx_x = 20;
  560. // _backBtn.hx_y = hxNavigationBarHeight - 15 - _backBtn.hx_h;
  561. [_backBtn addTarget:self action:@selector(didBackClick) forControlEvents:UIControlEventTouchUpInside];
  562. [_backBtn hx_setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
  563. }
  564. return _backBtn;
  565. }
  566. - (void)didBackClick {
  567. self.isCancel = YES;
  568. if (self.cancelBlock) {
  569. self.cancelBlock(self);
  570. }
  571. if ([self.delegate respondsToSelector:@selector(photoEditingControllerDidCancel:)]) {
  572. [self.delegate photoEditingControllerDidCancel:self];
  573. }
  574. if (!self.isAutoBack) {
  575. return;
  576. }
  577. if (self.navigationController.viewControllers.count <= 1) {
  578. [self dismissViewControllerAnimated:YES completion:nil];
  579. }else {
  580. [self.navigationController popViewControllerAnimated:YES];
  581. }
  582. }
  583. - (HX_PhotoEditBottomView *)toolsView {
  584. if (!_toolsView) {
  585. _toolsView = [HX_PhotoEditBottomView initView];
  586. _toolsView.themeColor = self.configuration.themeColor;
  587. _toolsView.frame = CGRectMake(0, self.view.hx_h - 50 - hxBottomMargin, self.view.hx_w, 50 + hxBottomMargin);
  588. HXWeakSelf
  589. _toolsView.didToolsBtnBlock = ^(NSInteger tag, BOOL isSelected) {
  590. [weakSelf.editingView.clippingView.imageView.stickerView removeSelectItem];
  591. if (tag == 0) {
  592. // 绘画
  593. [weakSelf.mosaicView removeFromSuperview];
  594. weakSelf.editingView.splashEnable = NO;
  595. weakSelf.editingView.drawEnable = isSelected;
  596. if (isSelected) {
  597. weakSelf.editingView.clippingView.imageView.type = HXPhotoEditImageViewTypeDraw;
  598. [weakSelf.view addSubview:weakSelf.graffitiColorView];
  599. [weakSelf.view addSubview:weakSelf.graffitiColorSizeView];
  600. }else {
  601. weakSelf.editingView.clippingView.imageView.type = HXPhotoEditImageViewTypeNormal;
  602. [weakSelf.graffitiColorView removeFromSuperview];
  603. [weakSelf.graffitiColorSizeView removeFromSuperview];
  604. }
  605. }else if (tag == 1) {
  606. // 贴图
  607. [HXPhotoEditChartletListView showEmojiViewWithConfiguration:weakSelf.configuration completion:^(UIImage * _Nonnull image) {
  608. HXPhotoEditStickerItem *item = [[HXPhotoEditStickerItem alloc] init];
  609. item.image = image;
  610. [weakSelf.editingView.clippingView.imageView.stickerView addStickerItem:item isSelected:YES];
  611. }];
  612. }else if (tag == 2) {
  613. // 文字
  614. [HXPhotoEditTextView showEitdTextViewWithConfiguration:weakSelf.configuration completion:^(HXPhotoEditTextModel * _Nonnull textModel) {
  615. HXPhotoEditStickerItem *item = [[HXPhotoEditStickerItem alloc] init];
  616. item.textModel = textModel;
  617. [weakSelf.editingView.clippingView.imageView.stickerView addStickerItem:item isSelected:YES];
  618. }];
  619. }else if (tag == 3) {
  620. // 裁剪
  621. [weakSelf.editingView photoEditEnable:!isSelected];
  622. weakSelf.tap.enabled = !isSelected;
  623. weakSelf.clippingToolBar.userInteractionEnabled = isSelected;
  624. [UIView animateWithDuration:0.25 animations:^{
  625. weakSelf.clippingToolBar.alpha = isSelected ? 1 : 0;
  626. }];
  627. [weakSelf.clippingToolBar setRotateAlpha:1.f];
  628. if (isSelected) {
  629. [weakSelf.editingView setClipping:YES animated:YES];
  630. [weakSelf hideBgViews];
  631. weakSelf.bottomMaskView.alpha = 1;
  632. }else {
  633. [weakSelf showBgViews];
  634. }
  635. }else if (tag == 4) {
  636. // 马赛克
  637. weakSelf.editingView.drawEnable = NO;
  638. weakSelf.editingView.splashEnable = isSelected;
  639. weakSelf.editingView.clippingView.imageView.type = HXPhotoEditImageViewTypeNormal;
  640. [weakSelf.graffitiColorView removeFromSuperview];
  641. [weakSelf.graffitiColorSizeView removeFromSuperview];
  642. if (isSelected) {
  643. weakSelf.editingView.clippingView.imageView.type = HXPhotoEditImageViewTypeSplash;
  644. [weakSelf.view addSubview:weakSelf.mosaicView];
  645. }else {
  646. weakSelf.editingView.clippingView.imageView.type = HXPhotoEditImageViewTypeNormal;
  647. [weakSelf.mosaicView removeFromSuperview];
  648. }
  649. }
  650. };
  651. _toolsView.didDoneBtnBlock = ^{
  652. [weakSelf startEditImage];
  653. };
  654. }
  655. return _toolsView;
  656. }
  657. - (void)startEditImage {
  658. self.view.userInteractionEnabled = NO;
  659. [self.view hx_showLoadingHUDText:nil];
  660. __block HXPhotoEdit *photoEdit = nil;
  661. NSDictionary *data = [self.editingView photoEditData];
  662. HXWeakSelf
  663. void (^finishImage)(UIImage *) = ^(UIImage *image){
  664. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  665. if (data) {
  666. NSString *fileName = [[NSString hx_fileName] stringByAppendingString:@".jpg"];
  667. NSString *fullPathToFile = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  668. NSData *imageData = HX_UIImageJPEGRepresentation(weakSelf.editImage);
  669. if ([imageData writeToFile:fullPathToFile atomically:YES]) {
  670. photoEdit = [[HXPhotoEdit alloc] initWithEditImagePath:fullPathToFile previewImage:image data:data];
  671. }
  672. }
  673. if (!photoEdit) {
  674. [weakSelf.photoModel.photoEdit clearData];
  675. }
  676. dispatch_async(dispatch_get_main_queue(), ^{
  677. weakSelf.photoModel.photoEdit = photoEdit;
  678. weakSelf.photoModel.assetByte = 0;
  679. if (weakSelf.saveAlbum) {
  680. UIImage *saveImage = photoEdit ? photoEdit.editPreviewImage : weakSelf.editImage;
  681. [HXPhotoTools savePhotoToCustomAlbumWithName:weakSelf.albumName photo:saveImage location:weakSelf.location complete:^(HXPhotoModel *model, BOOL success) {
  682. if (!success) {
  683. model = [HXPhotoModel photoModelWithImage:saveImage];
  684. }
  685. if ([weakSelf.delegate respondsToSelector:@selector(photoEditingController:didFinishPhotoEdit:photoModel:)]) {
  686. [weakSelf.delegate photoEditingController:weakSelf didFinishPhotoEdit:nil photoModel:model];
  687. }
  688. if (weakSelf.finishBlock) {
  689. weakSelf.finishBlock(nil, model, weakSelf);
  690. }
  691. [weakSelf dissmissClick];
  692. }];
  693. }else {
  694. if ([weakSelf.delegate respondsToSelector:@selector(photoEditingController:didFinishPhotoEdit:photoModel:)]) {
  695. [weakSelf.delegate photoEditingController:weakSelf didFinishPhotoEdit:photoEdit photoModel:weakSelf.photoModel];
  696. }
  697. if (weakSelf.finishBlock) {
  698. weakSelf.finishBlock(photoEdit, weakSelf.photoModel, weakSelf);
  699. }
  700. [weakSelf dissmissClick];
  701. }
  702. });
  703. });
  704. };
  705. [weakSelf.editingView.clippingView.imageView.stickerView removeSelectItem];
  706. [weakSelf.editingView createEditImage:^(UIImage *editImage) {
  707. finishImage(editImage);
  708. }];
  709. }
  710. - (void)dissmissClick {
  711. [self.view hx_handleLoading:NO];
  712. self.view.userInteractionEnabled = YES;
  713. if (!self.isAutoBack) {
  714. return;
  715. }
  716. if (self.navigationController.viewControllers.count <= 1) {
  717. [self dismissViewControllerAnimated:YES completion:nil];
  718. }else {
  719. [self.navigationController popViewControllerAnimated:YES];
  720. }
  721. }
  722. - (HXPhotoEditGraffitiColorSizeView *)graffitiColorSizeView {
  723. if (!_graffitiColorSizeView) {
  724. _graffitiColorSizeView = [HXPhotoEditGraffitiColorSizeView initView];
  725. _graffitiColorSizeView.frame = CGRectMake(self.view.hx_w - 50 - 12, 0, 50, 180);
  726. _graffitiColorSizeView.hx_centerY = self.view.hx_w / 2;
  727. HXWeakSelf
  728. _graffitiColorSizeView.changeColorSize = ^(CGFloat scale) {
  729. [UIView cancelPreviousPerformRequestsWithTarget:weakSelf];
  730. CGFloat maxWidth = weakSelf.configuration.brushLineMaxWidth;
  731. CGFloat minWidth = weakSelf.configuration.brushLineMinWidth;
  732. CGFloat lineWidth = minWidth + (maxWidth - minWidth) * scale / weakSelf.editingView.zoomScale;
  733. weakSelf.editingView.drawLineWidth = lineWidth;
  734. [weakSelf setBrushinePromptViewSize];
  735. if (weakSelf.brushLineWidthPromptView.hidden || weakSelf.brushLineWidthPromptView.alpha == 0) {
  736. weakSelf.brushLineWidthPromptView.hidden = NO;
  737. [UIView animateWithDuration:0.25 animations:^{
  738. weakSelf.brushLineWidthPromptView.alpha = 1;
  739. } completion:^(BOOL finished) {
  740. if (finished) {
  741. [weakSelf performSelector:@selector(hiddenBrushLineWidthPromptView) withObject:nil afterDelay:2.f inModes:@[NSRunLoopCommonModes]];
  742. }
  743. }];
  744. }else {
  745. [weakSelf performSelector:@selector(hiddenBrushLineWidthPromptView) withObject:nil afterDelay:2.f inModes:@[NSRunLoopCommonModes]];
  746. }
  747. };
  748. }
  749. return _graffitiColorSizeView;
  750. }
  751. - (void)hiddenBrushLineWidthPromptView {
  752. if (!self.brushLineWidthPromptView.hidden || self.brushLineWidthPromptView.alpha == 1) {
  753. [UIView animateWithDuration:0.25 animations:^{
  754. self.brushLineWidthPromptView.alpha = 0;
  755. } completion:^(BOOL finished) {
  756. if (finished) {
  757. self.brushLineWidthPromptView.hidden = YES;
  758. }
  759. }];
  760. }
  761. }
  762. - (UIView *)brushLineWidthPromptView {
  763. if (!_brushLineWidthPromptView) {
  764. _brushLineWidthPromptView = [[UIView alloc] initWithFrame:CGRectZero];
  765. _brushLineWidthPromptView.hidden = YES;
  766. _brushLineWidthPromptView.alpha = 0;
  767. UIColor *promptBgColor;
  768. if (self.configuration.drawColors.count > self.configuration.defaultDarwColorIndex) {
  769. promptBgColor = self.configuration.drawColors[self.configuration.defaultDarwColorIndex];
  770. }else {
  771. promptBgColor = self.configuration.drawColors.firstObject;
  772. }
  773. _brushLineWidthPromptView.layer.borderWidth = 2.f;
  774. if ([promptBgColor hx_colorIsWhite]) {
  775. _brushLineWidthPromptView.layer.borderColor = [UIColor hx_colorWithHexStr:@"#dadada"].CGColor;
  776. }else {
  777. _brushLineWidthPromptView.layer.borderColor = [UIColor whiteColor].CGColor;
  778. }
  779. _brushLineWidthPromptView.backgroundColor = promptBgColor;
  780. _brushLineWidthPromptView.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.6f].CGColor;
  781. _brushLineWidthPromptView.layer.shadowOffset = CGSizeMake(0, 0);
  782. _brushLineWidthPromptView.layer.shadowOpacity = 0.6f;
  783. }
  784. return _brushLineWidthPromptView;
  785. }
  786. - (void)setBrushinePromptViewSize {
  787. CGFloat maxWidth = self.configuration.brushLineMaxWidth;
  788. CGFloat minWidth = self.configuration.brushLineMinWidth;
  789. CGFloat width = self.graffitiColorSizeView.scale * (maxWidth - minWidth) + minWidth + 6 ;
  790. self.brushLineWidthPromptView.hx_size = CGSizeMake(width, width);
  791. self.brushLineWidthPromptView.layer.shadowRadius = width / 2.f;
  792. self.brushLineWidthPromptView.center = CGPointMake(self.view.hx_w / 2, self.view.hx_h / 2);
  793. if (HX_IOS11_Later) {
  794. [self.brushLineWidthPromptView hx_radiusWithRadius:width / 2.f corner:UIRectCornerAllCorners];
  795. }else {
  796. self.brushLineWidthPromptView.layer.cornerRadius = width / 2.f;
  797. }
  798. }
  799. - (HXPhotoEditGraffitiColorView *)graffitiColorView {
  800. if (!_graffitiColorView) {
  801. _graffitiColorView = [HXPhotoEditGraffitiColorView initView];
  802. _graffitiColorView.frame = CGRectMake(0, self.toolsView.hx_y - HXGraffitiColorViewHeight, self.view.hx_w, HXGraffitiColorViewHeight);
  803. _graffitiColorView.defaultDarwColorIndex = self.configuration.defaultDarwColorIndex;
  804. _graffitiColorView.drawColors = self.configuration.drawColors;
  805. HXWeakSelf
  806. _graffitiColorView.selectColorBlock = ^(UIColor * _Nonnull color) {
  807. UIColor *promptBgColor = color;
  808. weakSelf.editingView.clippingView.imageView.drawView.lineColor = color;
  809. if ([promptBgColor hx_colorIsWhite]) {
  810. weakSelf.brushLineWidthPromptView.layer.borderColor = [UIColor hx_colorWithHexStr:@"#dadada"].CGColor;
  811. }else {
  812. weakSelf.brushLineWidthPromptView.layer.borderColor = [UIColor whiteColor].CGColor;
  813. }
  814. weakSelf.brushLineWidthPromptView.backgroundColor = promptBgColor;
  815. };
  816. _graffitiColorView.undoBlock = ^{
  817. [weakSelf.editingView.clippingView.imageView.drawView undo];
  818. weakSelf.graffitiColorView.undo = weakSelf.editingView.clippingView.imageView.drawView.canUndo;
  819. };
  820. }
  821. return _graffitiColorView;
  822. }
  823. - (HXPhotoEditMosaicView *)mosaicView {
  824. if (!_mosaicView) {
  825. _mosaicView = [HXPhotoEditMosaicView initView];
  826. _mosaicView.themeColor = self.configuration.themeColor;
  827. _mosaicView.frame = CGRectMake(0, self.toolsView.hx_y - HXmosaicViewHeight, self.view.hx_w, HXmosaicViewHeight);
  828. HXWeakSelf
  829. _mosaicView.didBtnBlock = ^(NSInteger tag) {
  830. if (tag == 0) {
  831. weakSelf.editingView.clippingView.imageView.splashView.state = HXPhotoEditSplashStateType_Mosaic;
  832. }else {
  833. weakSelf.editingView.clippingView.imageView.splashView.state = HXPhotoEditSplashStateType_Paintbrush;
  834. }
  835. };
  836. _mosaicView.undoBlock = ^{
  837. [weakSelf.editingView.clippingView.imageView.splashView undo];
  838. weakSelf.mosaicView.undo = weakSelf.editingView.clippingView.imageView.splashView.canUndo;
  839. };
  840. }
  841. return _mosaicView;
  842. }
  843. - (HXPhotoEditingView *)editingView {
  844. if (!_editingView) {
  845. _editingView = [[HXPhotoEditingView alloc] initWithFrame:self.view.bounds config:self.configuration];
  846. _editingView.onlyCliping = self.onlyCliping;
  847. _editingView.clippingDelegate = self;
  848. if (self.configuration.drawColors.count > self.configuration.defaultDarwColorIndex) {
  849. _editingView.clippingView.imageView.drawView.lineColor = self.configuration.drawColors[self.configuration.defaultDarwColorIndex];
  850. }else {
  851. _editingView.clippingView.imageView.drawView.lineColor = self.configuration.drawColors.firstObject;
  852. }
  853. CGFloat maxWidth = self.configuration.brushLineMaxWidth;
  854. CGFloat minWidth = self.configuration.brushLineMinWidth;
  855. CGFloat drawLineWidth = minWidth + (maxWidth - minWidth) * 0.5f;
  856. _editingView.drawLineWidth = drawLineWidth;
  857. if (self.configuration.aspectRatio != HXPhotoEditAspectRatioType_None) {
  858. _editingView.fixedAspectRatio = YES;
  859. }
  860. _editingView.defaultAspectRatioIndex = self.configuration.aspectRatio;
  861. _editingView.customRatioSize = self.configuration.customAspectRatio;
  862. HXWeakSelf
  863. /** 模糊 */
  864. _editingView.clippingView.imageView.splashView.splashBegan = ^{
  865. [weakSelf hideBgViews];
  866. [UIView cancelPreviousPerformRequestsWithTarget:weakSelf];
  867. };
  868. _editingView.clippingView.imageView.splashView.splashEnded = ^{
  869. weakSelf.mosaicView.undo = YES;
  870. [weakSelf performSelector:@selector(showBgViews) withObject:nil afterDelay:.5f];
  871. };
  872. _editingView.clippingView.imageView.drawView.beganDraw = ^{
  873. // 开始绘画
  874. [weakSelf hideBgViews];
  875. [UIView cancelPreviousPerformRequestsWithTarget:weakSelf];
  876. };
  877. _editingView.clippingView.imageView.drawView.endDraw = ^{
  878. // 结束绘画
  879. weakSelf.graffitiColorView.undo = YES;
  880. [weakSelf performSelector:@selector(showBgViews) withObject:nil afterDelay:.5f];
  881. };
  882. _editingView.clippingView.imageView.stickerView.touchBegan = ^(HXPhotoEditStickerItemView * _Nonnull itemView) {
  883. [weakSelf hideBgViews];
  884. };
  885. _editingView.clippingView.imageView.stickerView.touchEnded = ^(HXPhotoEditStickerItemView * _Nonnull itemView) {
  886. [weakSelf showBgViews];
  887. };
  888. }
  889. return _editingView;
  890. }
  891. - (HXPhotoEditClippingToolBar *)clippingToolBar {
  892. if (!_clippingToolBar) {
  893. _clippingToolBar = [HXPhotoEditClippingToolBar initView];
  894. if (self.configuration.aspectRatio != HXPhotoEditAspectRatioType_None) {
  895. _clippingToolBar.enableRotaio = NO;
  896. }else {
  897. _clippingToolBar.enableRotaio = YES;
  898. }
  899. _clippingToolBar.themeColor = self.configuration.themeColor;
  900. _clippingToolBar.userInteractionEnabled = NO;
  901. _clippingToolBar.alpha = 0;
  902. _clippingToolBar.frame = CGRectMake(0, self.view.hx_h - HXClippingToolBar - hxBottomMargin, self.view.hx_w, HXClippingToolBar + hxBottomMargin);
  903. HXWeakSelf
  904. _clippingToolBar.didRotateBlock = ^{
  905. [weakSelf.editingView rotate];
  906. weakSelf.clippingToolBar.enableReset = weakSelf.editingView.canReset;
  907. };
  908. _clippingToolBar.didMirrorHorizontallyBlock = ^{
  909. [weakSelf.editingView mirrorFlip];
  910. };
  911. _clippingToolBar.selectedRotaioBlock = ^(HXPhotoEditClippingToolBarRotaioModel * _Nonnull model) {
  912. if (model.widthRatio) {
  913. weakSelf.editingView.clippingView.fixedAspectRatio = YES;
  914. weakSelf.editingView.customRatioSize = CGSizeMake(model.widthRatio, model.heightRatio);
  915. [weakSelf.editingView resetToRridRectWithAspectRatioIndex:HXPhotoEditAspectRatioType_Custom];
  916. }else {
  917. weakSelf.editingView.clippingView.fixedAspectRatio = NO;
  918. [weakSelf.editingView resetToRridRectWithAspectRatioIndex:0];
  919. weakSelf.clippingToolBar.enableReset = weakSelf.editingView.canReset;
  920. }
  921. };
  922. _clippingToolBar.didBtnBlock = ^(NSInteger tag) {
  923. BOOL aspectRotioNone = weakSelf.configuration.aspectRatio == HXPhotoEditAspectRatioType_None;
  924. if (tag == 0) {
  925. if (aspectRotioNone) {
  926. weakSelf.editingView.clippingView.fixedAspectRatio = NO;
  927. [weakSelf.editingView resetToRridRectWithAspectRatioIndex:0];
  928. }
  929. // 取消
  930. if (weakSelf.onlyCliping) {
  931. [weakSelf didBackClick];
  932. return;
  933. }
  934. [weakSelf.toolsView endCliping];
  935. [weakSelf.editingView cancelClipping:YES];
  936. }else if (tag == 1) {
  937. // 确认
  938. if (weakSelf.onlyCliping) {
  939. [weakSelf startEditImage];
  940. return;
  941. }
  942. [weakSelf.toolsView endCliping];
  943. [weakSelf.editingView setClipping:NO animated:YES completion:^{
  944. if (aspectRotioNone) {
  945. weakSelf.editingView.clippingView.fixedAspectRatio = NO;
  946. [weakSelf.editingView resetToRridRectWithAspectRatioIndex:0];
  947. }
  948. }];
  949. }else if (tag == 2) {
  950. // 还原
  951. if (aspectRotioNone) {
  952. weakSelf.editingView.clippingView.fixedAspectRatio = NO;
  953. [weakSelf.editingView resetToRridRectWithAspectRatioIndex:0];
  954. }
  955. [weakSelf.editingView reset];
  956. weakSelf.clippingToolBar.enableReset = weakSelf.editingView.canReset;
  957. }
  958. };
  959. }
  960. return _clippingToolBar;
  961. }
  962. - (UIView *)topMaskView {
  963. if (!_topMaskView) {
  964. _topMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HX_ScreenWidth, hxNavigationBarHeight)];
  965. self.topMaskLayer.frame = CGRectMake(0, 0, HX_ScreenWidth, hxNavigationBarHeight + 30.f);
  966. [_topMaskView.layer addSublayer:self.topMaskLayer];
  967. }
  968. return _topMaskView;
  969. }
  970. - (CAGradientLayer *)topMaskLayer {
  971. if (!_topMaskLayer) {
  972. _topMaskLayer = [CAGradientLayer layer];
  973. _topMaskLayer.colors = @[
  974. (id)[[UIColor blackColor] colorWithAlphaComponent:0].CGColor,
  975. (id)[[UIColor blackColor] colorWithAlphaComponent:0.3].CGColor
  976. ];
  977. _topMaskLayer.startPoint = CGPointMake(0, 1);
  978. _topMaskLayer.endPoint = CGPointMake(0, 0);
  979. _topMaskLayer.locations = @[@(0.15f),@(0.9f)];
  980. _topMaskLayer.borderWidth = 0.0;
  981. }
  982. return _topMaskLayer;
  983. }
  984. - (UIView *)bottomMaskView {
  985. if (!_bottomMaskView) {
  986. _bottomMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, HX_ScreenHeight - hxBottomMargin - 120, HX_ScreenWidth, hxBottomMargin + 120)];
  987. _bottomMaskView.userInteractionEnabled = NO;
  988. self.bottomMaskLayer.frame = _bottomMaskView.bounds;
  989. [_bottomMaskView.layer addSublayer:self.bottomMaskLayer];
  990. }
  991. return _bottomMaskView;;
  992. }
  993. - (CAGradientLayer *)bottomMaskLayer {
  994. if (!_bottomMaskLayer) {
  995. _bottomMaskLayer = [CAGradientLayer layer];
  996. _bottomMaskLayer.colors = @[
  997. (id)[[UIColor blackColor] colorWithAlphaComponent:0].CGColor,
  998. (id)[[UIColor blackColor] colorWithAlphaComponent:0.4].CGColor
  999. ];
  1000. _bottomMaskLayer.startPoint = CGPointMake(0, 0);
  1001. _bottomMaskLayer.endPoint = CGPointMake(0, 1);
  1002. _bottomMaskLayer.locations = @[@(0),@(1.f)];
  1003. _bottomMaskLayer.borderWidth = 0.0;
  1004. }
  1005. return _bottomMaskLayer;
  1006. }
  1007. - (HXPhotoEditConfiguration *)configuration {
  1008. if (!_configuration) {
  1009. _configuration = [[HXPhotoEditConfiguration alloc] init];
  1010. }
  1011. return _configuration;
  1012. }
  1013. @end