CPDFImageViewController.m 16 KB

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