PDFImageViewController.m 17 KB

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