PDFImageWatermarkController.m 18 KB

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