Kaynağa Gözat

PDFViewer(ios) - 基本实现水印功能的绑定(bug待优化)

dengkang 2 yıl önce
ebeveyn
işleme
0c711d7057

BIN
PDFViewer.xcodeproj/project.xcworkspace/xcuserdata/kdan.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 0
PDFViewer/Controller/CPDFImageViewController.h

@@ -26,6 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
 - (void)createGestureRecognizer;
 - (void)createCustomRangeAlert;
 
+- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 32 - 6
PDFViewer/Controller/CPDFImageViewController.m

@@ -11,7 +11,7 @@
 #import "CPDFDataModel.h"
 #import "Masonry.h"
 
-@interface CPDFImageViewController ()
+@interface CPDFImageViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
 
 @property (nonatomic,assign) CGRect watermarkFrame;
 
@@ -43,11 +43,11 @@
     if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
         
         [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
-            make.right.equalTo(self.view.mas_right).offset(-30);
+            make.right.equalTo(self.view.mas_right).offset(-38);
             make.left.equalTo(_imagePreview.mas_right).offset(0);
             make.height.equalTo(@205);
             make.width.equalTo(@(self.view.bounds.size.width));
-            make.bottom.equalTo(self.view.mas_bottom).offset(-55);
+            make.bottom.equalTo(self.view.mas_bottom).offset(-40);
         }];
         [_imagePreview mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.view.mas_top).offset(10);
@@ -141,13 +141,17 @@
         [_imagePreview.rotationBtn setCenter:CGPointMake(_imagePreview.rotationBtn.center.x - point.x, _imagePreview.rotationBtn.center.y - point.y)];
     }
     
-    _dataModel.tx = _imagePreview.watermarkView.frame.origin.x - (_imagePreview.documentView.center.x - _imagePreview.documentView.frame.origin.x);
-    _dataModel.ty = _imagePreview.documentView.center.y - _imagePreview.documentView.frame.origin.y - _imagePreview.watermarkView.frame.origin.y;
+    _dataModel.tx = _imagePreview.watermarkView.center.x - (_imagePreview.documentView.center.x - _imagePreview.documentView.frame.origin.x);
+    _dataModel.ty = _imagePreview.documentView.center.y - _imagePreview.documentView.frame.origin.y - _imagePreview.watermarkView.center.y;
     [recognizer setTranslation:CGPointZero inView:_imagePreview.documentView];
 }
 
 - (void)onSelectBtnClicked:(UIButton *)sender {
-    
+    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
+    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
+    imagePicker.delegate = self;
+    imagePicker.allowsEditing = YES;
+    [self presentViewController:imagePicker animated:YES completion:nil];
 }
 
 - (void)onOpacityChanged:(UISlider *)sender {
@@ -222,4 +226,26 @@
     }
 }
 
+#pragma mark - UIImagePickerControllerDelegate
+
+- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
+    UIImage *image = info[UIImagePickerControllerOriginalImage];
+    _imagePreview.watermarkView.image = [self imageWithImageSimple:image scaledToSize:CGSizeMake(120, 120)];
+    [_imagePreview.watermarkView sizeToFit];
+    [picker dismissViewControllerAnimated:YES completion:nil];
+}
+
+- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
+    [picker dismissViewControllerAnimated:YES completion:nil];
+}
+
+- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
+{
+    UIGraphicsBeginImageContext(newSize);
+    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
+    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+    return newImage;
+}
+
 @end

+ 0 - 1
PDFViewer/Controller/CPDFTextViewController.h

@@ -24,7 +24,6 @@ NS_ASSUME_NONNULL_BEGIN
 - (void)initDataModel;
 - (void)addTargets;
 - (void)addConstraint;
-- (void)watermarkAdaption;
 - (void)createGestureRecognizer;
 - (void)createCustomRangeAlert;
 

+ 7 - 22
PDFViewer/Controller/CPDFTextViewController.m

@@ -13,8 +13,6 @@
 
 @interface CPDFTextViewController ()
 
-@property (nonatomic,assign) CGRect watermarkFrame;
-
 @end
 
 @implementation CPDFTextViewController
@@ -45,11 +43,11 @@
     if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
         
         [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
-            make.right.equalTo(self.view.mas_right).offset(-30);
+            make.right.equalTo(self.view.mas_right).offset(-38);
             make.left.equalTo(_textPreview.mas_right).offset(0);
             make.height.equalTo(@205);
             make.width.equalTo(@(self.view.bounds.size.width));
-            make.bottom.equalTo(self.view.mas_bottom).offset(-55);
+            make.bottom.equalTo(self.view.mas_bottom).offset(-40);
         }];
         [_textPreview mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.view.mas_top).offset(10);
@@ -64,12 +62,12 @@
             make.width.equalTo(self.view.mas_width);
             make.bottom.equalTo(self.view.mas_bottom).offset(-205);
         }];
-
         [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(_textPreview.mas_bottom).offset(0);
             make.width.equalTo(_textPreview.mas_width);
             make.bottom.equalTo(self.view.mas_bottom).offset(0);
         }];
+        
     }
 }
 
@@ -115,7 +113,7 @@
         
         strongBlock.dataModel.text = strongBlock.alertController.textFields.firstObject.text;
         strongBlock.textPreview.watermarkLabel.text = strongBlock.alertController.textFields.firstObject.text;
-        [strongBlock watermarkAdaption];
+        [strongBlock.textPreview.watermarkLabel sizeToFit];
         
     }]];
     [_alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
@@ -158,7 +156,7 @@
         sender.minimumValue = 24;
         sender.maximumValue = 48;
         _textPreview.watermarkLabel.font = [_textPreview.watermarkLabel.font fontWithSize:sender.value];
-        [self watermarkAdaption];
+        [_textPreview.watermarkLabel sizeToFit];
         [_dataModel setWatermarkScale:sender.value / 24];
     }
 }
@@ -210,17 +208,6 @@
     [self presentViewController:alertController animated:YES completion:nil];
 }
 
-- (void)watermarkAdaption {
-    [_textPreview.watermarkLabel sizeToFit];
-    
-    [_textPreview.rotationBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
-        make.top.equalTo(_textPreview.watermarkLabel.mas_bottom).offset(-10);
-        make.width.equalTo(@20);
-        make.height.equalTo(@20);
-        make.left.equalTo(_textPreview.watermarkLabel.mas_right).offset(-10);
-    }];
-}
-
 - (void)tapWatermarkLabel:(UITapGestureRecognizer *)recognizer {
     _alertController.view.hidden = NO;
     _alertController.textFields.firstObject.text = _textPreview.watermarkLabel.text;
@@ -239,17 +226,15 @@
         [_textPreview.rotationBtn setCenter:CGPointMake(_textPreview.rotationBtn.center.x - point.x, _textPreview.rotationBtn.center.y - point.y)];
     }
     
-    _dataModel.tx = _textPreview.watermarkLabel.frame.origin.x - (_textPreview.documentView.center.x - _textPreview.documentView.frame.origin.x);
-    _dataModel.ty = _textPreview.documentView.center.y - _textPreview.documentView.frame.origin.y - _textPreview.watermarkLabel.frame.origin.y;
+    _dataModel.tx = _textPreview.watermarkLabel.center.x - (_textPreview.documentView.center.x - _textPreview.documentView.frame.origin.x);
+    _dataModel.ty = _textPreview.documentView.center.y - _textPreview.documentView.frame.origin.y - _textPreview.watermarkLabel.center.y;
     [recognizer setTranslation:CGPointZero inView:_textPreview.documentView];
 }
 
 - (void)rotationWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
-    
     CGPoint point = [recognizer translationInView:_textPreview];
     CGFloat radian = atan2(point.x + _textPreview.center.x - _textPreview.watermarkLabel.center.x,point.y + _textPreview.center.y - _textPreview.watermarkLabel.center.y);
     _textPreview.watermarkLabel.transform = CGAffineTransformMakeRotation(-radian);
-    
     _dataModel.watermarkRotation = (-radian) * 180 / M_PI;
 }
 

+ 14 - 14
PDFViewer/Controller/CPDFViewController.m

@@ -116,12 +116,12 @@
     [_textViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
         make.width.equalTo(_segmentedControl.mas_width);
-        make.bottom.equalTo(self.view).offset(0);
+        make.bottom.equalTo(self.view).offset(-34);
     }];
     [_imageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
         make.width.equalTo(_segmentedControl.mas_width);
-        make.bottom.equalTo(self.view).offset(0);
+        make.bottom.equalTo(self.view).offset(-34);
     }];
     [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
@@ -145,27 +145,27 @@
             [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
                 make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
-                make.width.equalTo(@(_imageSize.width / 3.1));
-                make.height.equalTo(@(_imageSize.height / 2.6));
+                make.width.equalTo(@(_imageSize.width / 3));
+                make.height.equalTo(@(_imageSize.height / 3));
             }];
             [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
                 make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
-                make.width.equalTo(@(_imageSize.width / 3.1));
-                make.height.equalTo(@(_imageSize.height / 2.6));
+                make.width.equalTo(@(_imageSize.width / 3));
+                make.height.equalTo(@(_imageSize.height / 3));
             }];
         } else {
             [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
                 make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
-                make.width.equalTo(@(_imageSize.width / 2.4));
-                make.height.equalTo(@(_imageSize.height / 2.9));
+                make.width.equalTo(@(_imageSize.width / 3));
+                make.height.equalTo(@(_imageSize.height / 3.3));
             }];
             [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
                 make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
-                make.width.equalTo(@(_imageSize.width / 2.4));
-                make.height.equalTo(@(_imageSize.height / 2.9));
+                make.width.equalTo(@(_imageSize.width / 3));
+                make.height.equalTo(@(_imageSize.height / 3.3));
             }];
         }
         
@@ -221,8 +221,8 @@
         _textWatermark.opacity = _textViewController.dataModel.watermarkOpacity;
         _textWatermark.scale = _textViewController.dataModel.watermarkScale;
         _textWatermark.isTilePage = _textViewController.dataModel.isTile;
-        _textWatermark.tx = _textViewController.dataModel.tx;
-        _textWatermark.ty = _textViewController.dataModel.ty;
+        _textWatermark.tx = _textViewController.dataModel.tx * _imageSize.width / _textViewController.textPreview.documentView.frame.size.width;
+        _textWatermark.ty = _textViewController.dataModel.ty * _imageSize.height / _textViewController.textPreview.documentView.frame.size.height;
         _textWatermark.rotation = _textViewController.dataModel.watermarkRotation;
         
         if (_textWatermark.isTilePage) {
@@ -245,8 +245,8 @@
         _imageWatermark.opacity = _imageViewController.dataModel.watermarkOpacity;
         _imageWatermark.scale = _imageViewController.dataModel.watermarkScale;
         _imageWatermark.isTilePage = _imageViewController.dataModel.isTile;
-        _imageWatermark.tx = _imageViewController.dataModel.tx;
-        _imageWatermark.ty = _imageViewController.dataModel.ty;
+        _imageWatermark.tx = _imageViewController.dataModel.tx * _imageSize.width / _imageViewController.imagePreview.documentView.frame.size.width;
+        _imageWatermark.ty = _imageViewController.dataModel.ty * _imageSize.height / _imageViewController.imagePreview.documentView.frame.size.height;
         _imageWatermark.rotation = _imageViewController.dataModel.watermarkRotation;
         
         if (_imageWatermark.isTilePage) {