CPDFImageViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // CPDFImageViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/19.
  6. //
  7. #import "CPDFImageViewController.h"
  8. #import "CPDFImageView.h"
  9. #import "CPDFImagePreview.h"
  10. #import "CPDFDataModel.h"
  11. #import "CPDFDrawView.h"
  12. #import "CPDFClipView.h"
  13. #import "Masonry.h"
  14. @interface CPDFImageViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,CPDFClipTextPreviewDelegate,UITextFieldDelegate>
  15. @property (nonatomic,assign) CGRect watermarkFrame;
  16. @property (nonatomic,strong) CPDFDrawView *drawView;
  17. @property (nonatomic,strong) CPDFClipView *cliView;
  18. @property (nonatomic,assign) CGAffineTransform transform1;
  19. @property (nonatomic,assign) CGAffineTransform transform2;
  20. @property (nonatomic,assign) CGRect normalWatermarkRect;
  21. @property (nonatomic,strong) UILabel *shadowWaterLabel;
  22. - (void)initDataModel;
  23. - (void)addTargets;
  24. - (void)addConstraint;
  25. - (void)createGestureRecognizer;
  26. - (void)createCustomRangeAlert;
  27. @end
  28. @implementation CPDFImageViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. _imagePreview = [[CPDFImagePreview alloc] init];
  33. _imageView = [[CPDFImageView alloc] init];
  34. _dataModel = [[CPDFDataModel alloc] init];
  35. _imageView.verticalField.delegate = self;
  36. _imageView.horizontalField.delegate = self;
  37. [self.view addSubview:_imagePreview];
  38. [self.view addSubview:_imageView];
  39. [self.view setBackgroundColor:UIColor.systemGray5Color];
  40. [self initDataModel];
  41. [self addConstraint];
  42. [self addTargets];
  43. [self createGestureRecognizer];
  44. _normalWatermarkRect = CGRectMake(97.5, 143.333, 28, 28);
  45. _shadowWaterLabel = [[UILabel alloc] initWithFrame:CGRectMake(97.5, 143.333, 28, 28)];
  46. }
  47. #pragma mark - Orientation
  48. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  49. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  50. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  51. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  52. make.right.equalTo(self.view.mas_right).offset(-38);
  53. make.left.equalTo(_imagePreview.mas_right).offset(0);
  54. make.height.equalTo(@205);
  55. make.width.equalTo(@(self.view.bounds.size.width));
  56. make.bottom.equalTo(self.view.mas_bottom).offset(-40);
  57. }];
  58. [_imagePreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  59. make.top.equalTo(self.view.mas_top).offset(0);
  60. make.right.equalTo(_imageView.mas_left).offset(-5);
  61. make.left.equalTo(self.view.mas_left).offset(0);
  62. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  63. }];
  64. if ([_drawView isDescendantOfView:self.view]) {
  65. [_drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
  66. make.top.equalTo(self.view.mas_top).offset(0);
  67. make.right.equalTo(_imageView.mas_left).offset(-5);
  68. make.left.equalTo(self.view.mas_left).offset(0);
  69. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  70. }];
  71. [_cliView mas_remakeConstraints:^(MASConstraintMaker *make) {
  72. make.top.equalTo(self.view.mas_top).offset(0);
  73. make.right.equalTo(_imageView.mas_left).offset(-5);
  74. make.left.equalTo(self.view.mas_left).offset(0);
  75. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  76. }];
  77. }
  78. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  79. [_imagePreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(self.view.mas_top).offset(0);
  81. make.width.equalTo(self.view.mas_width);
  82. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  83. }];
  84. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  85. make.top.equalTo(_imagePreview.mas_bottom).offset(0);
  86. make.width.equalTo(_imagePreview.mas_width);
  87. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  88. }];
  89. if ([_drawView isDescendantOfView:self.view]) {
  90. [_drawView mas_remakeConstraints:^(MASConstraintMaker *make) {
  91. make.top.equalTo(self.view.mas_top).offset(0);
  92. make.width.equalTo(self.view.mas_width);
  93. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  94. }];
  95. [_cliView mas_remakeConstraints:^(MASConstraintMaker *make) {
  96. make.top.equalTo(self.view.mas_top).offset(0);
  97. make.width.equalTo(self.view.mas_width);
  98. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  99. }];
  100. }
  101. }
  102. if ([_drawView isDescendantOfView:self.view]) {
  103. [_drawView setDocumentViewRect:_imagePreview.documentView.frame];
  104. [_drawView setWaterLabelRect:_imagePreview.watermarkView.frame];
  105. [_drawView setNeedsDisplay];
  106. [_cliView setNeedsDisplay];
  107. }
  108. }
  109. #pragma mark - Initializers
  110. - (void)initDataModel {
  111. _dataModel.watermarkOpacity = 1;
  112. _dataModel.imagePath = @"btn_selected";
  113. _dataModel.image = [UIImage imageNamed:_dataModel.imagePath];
  114. _dataModel.watermarkScale = 1;
  115. _dataModel.isTile = NO;
  116. _dataModel.watermarkRotation = 0;
  117. _dataModel.horizontalSpacing = 100;
  118. _dataModel.verticalSpacing = 100;
  119. _imageView.horizontalField.text = 0;
  120. _imageView.verticalField.text = 0;
  121. _imageView.horizontalField.enabled = NO;
  122. _imageView.verticalField.enabled = NO;
  123. _imageView.imageScaleSlider.enabled = YES;
  124. _imagePreview.watermarkView.image = [UIImage imageNamed:_dataModel.imagePath];
  125. }
  126. - (void)addConstraint {
  127. [_imagePreview mas_makeConstraints:^(MASConstraintMaker *make) {
  128. make.top.equalTo(self.view.mas_top).offset(0);
  129. make.width.equalTo(self.view.mas_width);
  130. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  131. }];
  132. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.top.equalTo(_imagePreview.mas_bottom).offset(0);
  134. make.width.equalTo(_imagePreview.mas_width);
  135. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  136. }];
  137. }
  138. - (void)addTargets {
  139. [_imageView.selectBtn addTarget:self action:@selector(onSelectBtnClicked:) forControlEvents:UIControlEventTouchDown];
  140. [_imageView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
  141. [_imageView.imageScaleSlider addTarget:self action:@selector(onImageScaleChanged:) forControlEvents:UIControlEventValueChanged];
  142. [_imageView.tileSwitch addTarget:self action:@selector(onTileSwitchChanged:) forControlEvents:UIControlEventValueChanged];
  143. [_imageView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchDown];
  144. [_imageView.horizontalField addTarget:self action:@selector(horizontalChange:) forControlEvents:UIControlEventEditingDidEnd];
  145. [_imageView.verticalField addTarget:self action:@selector(verticalChage:) forControlEvents:UIControlEventEditingDidEnd];
  146. }
  147. #pragma mark - Gesture
  148. - (void)createGestureRecognizer {
  149. [_imagePreview.documentView setUserInteractionEnabled:YES];
  150. [_imagePreview.watermarkView setMultipleTouchEnabled:YES];
  151. [_imagePreview.watermarkView setUserInteractionEnabled:YES];
  152. [_imagePreview.rotationBtn setUserInteractionEnabled:YES];
  153. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkView:)];
  154. [_imagePreview.watermarkView addGestureRecognizer:panRecognizer];
  155. UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateWatermarkView:)];
  156. [_imagePreview.rotationBtn addGestureRecognizer:panRotationBtnRecognizer];
  157. }
  158. - (void)rotateWatermarkView:(UIPanGestureRecognizer *)recognizer {
  159. CGPoint point = [recognizer translationInView:_imagePreview];
  160. CGFloat radian = atan2(point.x + _imagePreview.center.x - _imagePreview.watermarkView.center.x,point.y + _imagePreview.center.y - _imagePreview.watermarkView.center.y);
  161. if (self.dataModel.watermarkScale == 1) {
  162. _imagePreview.watermarkView.transform = CGAffineTransformMakeRotation(-radian);
  163. } else {
  164. _imagePreview.watermarkView.transform = CGAffineTransformRotate(self.transform2, -radian);
  165. }
  166. _transform1 = CGAffineTransformMakeRotation(-radian);
  167. _dataModel.watermarkRotation = (-radian) * 180 / M_PI;
  168. }
  169. - (void)panWatermarkView:(UIPanGestureRecognizer *)recognizer {
  170. CGPoint point = [recognizer translationInView:_imagePreview.documentView];
  171. CGRect documentFrame = _imagePreview.documentView.frame;
  172. documentFrame.origin.x -= _imagePreview.documentView.frame.origin.x;
  173. documentFrame.origin.y -= _imagePreview.documentView.frame.origin.y;
  174. [_imagePreview.watermarkView setCenter:CGPointMake(_imagePreview.watermarkView.center.x + point.x, _imagePreview.watermarkView.center.y + point.y)];
  175. [_imagePreview.rotationBtn setCenter:CGPointMake(_imagePreview.rotationBtn.center.x + point.x, _imagePreview.rotationBtn.center.y + point.y)];
  176. if (!CGRectContainsRect(documentFrame,_imagePreview.watermarkView.frame)) {
  177. [_imagePreview.watermarkView setCenter:CGPointMake(_imagePreview.watermarkView.center.x - point.x, _imagePreview.watermarkView.center.y - point.y)];
  178. [_imagePreview.rotationBtn setCenter:CGPointMake(_imagePreview.rotationBtn.center.x - point.x, _imagePreview.rotationBtn.center.y - point.y)];
  179. }
  180. _dataModel.tx = _imagePreview.watermarkView.center.x - (_imagePreview.documentView.center.x - _imagePreview.documentView.frame.origin.x);
  181. _dataModel.ty = _imagePreview.documentView.center.y - _imagePreview.documentView.frame.origin.y - _imagePreview.watermarkView.center.y;
  182. _shadowWaterLabel.center = _imagePreview.watermarkView.center;
  183. [recognizer setTranslation:CGPointZero inView:_imagePreview.documentView];
  184. _normalWatermarkRect = _shadowWaterLabel.frame;
  185. }
  186. #pragma mark - UI Functions
  187. - (void)onSelectBtnClicked:(UIButton *)sender {
  188. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  189. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  190. imagePicker.delegate = self;
  191. imagePicker.allowsEditing = YES;
  192. [self presentViewController:imagePicker animated:YES completion:nil];
  193. }
  194. - (void)onOpacityChanged:(UISlider *)sender {
  195. _imagePreview.watermarkView.alpha = 1 - sender.value;
  196. [_dataModel setWatermarkOpacity:1 - sender.value];
  197. }
  198. /*
  199. this had core was deleted:
  200. // _watermarkFrame.size.width = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.width;
  201. // _watermarkFrame.size.height = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.height;
  202. // _watermarkFrame.size.width = sender.value * 33;
  203. // _watermarkFrame.size.height = sender.value * 33;
  204. //
  205. // _watermarkFrame.origin.x = _imagePreview.watermarkView.frame.origin.x;
  206. // _watermarkFrame.origin.y = _imagePreview.watermarkView.frame.origin.y;
  207. // _imagePreview.watermarkView.frame = _watermarkFrame;
  208. // _imagePreview.watermarkFrame = _watermarkFrame;
  209. */
  210. - (void)onImageScaleChanged:(UISlider *)sender {
  211. sender.minimumValue = 0.5;
  212. sender.maximumValue = 2;
  213. if (self.dataModel.watermarkRotation) {
  214. _imagePreview.watermarkView.transform = CGAffineTransformScale(self.transform1, sender.value, sender.value);
  215. } else {
  216. _imagePreview.watermarkView.transform = CGAffineTransformMakeScale(sender.value, sender.value);
  217. }
  218. _transform2 = CGAffineTransformMakeScale(sender.value, sender.value);
  219. _shadowWaterLabel.transform = CGAffineTransformMakeScale(sender.value, sender.value);
  220. [_imagePreview.rotationBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  221. make.top.equalTo(_imagePreview.watermarkView.mas_bottom).offset(-10);
  222. make.width.equalTo(@20);
  223. make.height.equalTo(@20);
  224. make.left.equalTo(_imagePreview.watermarkView.mas_right).offset(-10);
  225. }];
  226. [_dataModel setWatermarkScale:sender.value];
  227. _normalWatermarkRect = _shadowWaterLabel.frame;
  228. }
  229. - (void)onSelectPageRange:(UIButton *)sender {
  230. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  231. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  232. self.imageView.rangeLabel.text = @"Page Range: ALL";
  233. }];
  234. UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom Page Range" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  235. [self createCustomRangeAlert];
  236. }];
  237. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  238. }];
  239. [alertController addAction:defaultRange];
  240. [alertController addAction:customRange];
  241. [alertController addAction:cancelAction];
  242. [self presentViewController:alertController animated:YES completion:nil];
  243. }
  244. - (void)createCustomRangeAlert {
  245. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
  246. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  247. textField.placeholder = @"such as:1,3-5,10";
  248. }];
  249. [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  250. self.dataModel.pageString = alertController.textFields.firstObject.text;
  251. self.imageView.rangeLabel.text = @"Page Range:Custom";
  252. }]];
  253. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  254. }]];
  255. [self presentViewController:alertController animated:YES completion:nil];
  256. }
  257. - (void)onTileSwitchChanged:(UISwitch *) sender {
  258. if ([sender isOn]) {
  259. _dataModel.isTile = YES;
  260. _imageView.horizontalField.enabled = YES;
  261. _imageView.verticalField.enabled = YES;
  262. _imageView.imageScaleSlider.enabled = NO;
  263. _drawView = [[CPDFDrawView alloc] initWithFrame:self.imagePreview.bounds];
  264. [_drawView setDataModel:self.dataModel];
  265. [_drawView setWaterLabelRect:self.normalWatermarkRect];
  266. [_drawView setDocumentViewRect:self.imagePreview.documentView.frame];
  267. [self.view addSubview:_drawView];
  268. [self.imagePreview bringSubviewToFront:_drawView];
  269. _cliView = [[CPDFClipView alloc] initWithFrame:self.imagePreview.bounds];
  270. _cliView.delegate = self;
  271. [self.view addSubview:_cliView];
  272. [self.drawView bringSubviewToFront:_cliView];
  273. } else {
  274. _dataModel.isTile = NO;
  275. _imageView.horizontalField.enabled = NO;
  276. _imageView.verticalField.enabled = NO;
  277. _imageView.imageScaleSlider.enabled = YES;
  278. [self.drawView removeFromSuperview];
  279. [self.cliView removeFromSuperview];
  280. }
  281. }
  282. - (void)horizontalChange:(UITextField *)text {
  283. [_drawView setHorizontal:[text.text floatValue]];
  284. _dataModel.horizontalSpacing = [text.text floatValue];
  285. [_drawView setNeedsDisplay];
  286. }
  287. - (void)verticalChage:(UITextField *)text {
  288. [_drawView setVertical:[text.text floatValue]];
  289. _dataModel.verticalSpacing = [text.text floatValue];
  290. [_drawView setNeedsDisplay];
  291. }
  292. #pragma mark - UIImagePickerControllerDelegate
  293. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  294. UIImage *image = info[UIImagePickerControllerOriginalImage];
  295. CGSize size;
  296. size.width = image.size.width / 30;
  297. size.height = image.size.height / 30;
  298. _imagePreview.watermarkView.image = [self imageWithImageSimple:image scaledToSize:size];
  299. _dataModel.image = [self imageWithImageSimple:image scaledToSize:size];
  300. [_imagePreview.watermarkView sizeToFit];
  301. _shadowWaterLabel.frame = _imagePreview.watermarkView.frame;
  302. [picker dismissViewControllerAnimated:YES completion:nil];
  303. _normalWatermarkRect = _shadowWaterLabel.frame;
  304. }
  305. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  306. [picker dismissViewControllerAnimated:YES completion:nil];
  307. }
  308. - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
  309. {
  310. UIGraphicsBeginImageContext(newSize);
  311. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  312. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  313. UIGraphicsEndImageContext();
  314. return newImage;
  315. }
  316. #pragma mark - CPDFClipTextPreviewDelegate
  317. - (void)clipText:(CGContextRef)context {
  318. CGContextSetFillColorWithColor(context, [UIColor systemGray5Color].CGColor);
  319. CGContextFillRect(context, self.imagePreview.bounds);
  320. CGContextClearRect(context, self.imagePreview.documentView.frame);
  321. }
  322. #pragma mark - UITextFieldDelegate
  323. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  324. self.cliView.hidden = YES;
  325. self.drawView.hidden = YES;
  326. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  327. } else {
  328. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  329. make.height.mas_equalTo(205);
  330. make.width.equalTo(_imagePreview.mas_width);
  331. make.bottom.equalTo(self.view.mas_bottom).offset(-300);
  332. }];
  333. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  334. self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y - 300);
  335. } completion:nil];
  336. }
  337. }
  338. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  339. [textField resignFirstResponder];
  340. self.cliView.hidden = NO;
  341. self.drawView.hidden = NO;
  342. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  343. } else {
  344. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  345. make.top.equalTo(_imagePreview.mas_bottom).offset(0);
  346. make.width.equalTo(_imagePreview.mas_width);
  347. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  348. make.height.mas_equalTo(205);
  349. }];
  350. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  351. self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y + 300);
  352. } completion:nil];
  353. }
  354. return YES;
  355. }
  356. @end