CPDFImageViewController.m 17 KB

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