CPDFImageViewController.m 20 KB

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