CPDFImageViewController.m 14 KB

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