简体   繁体   中英

Annotation(Image & Text) subviews changes position after saving

I have created a PDF based application. Firstly, I get the PDF from document directory then convert it to Images and then programmatically set all images to vertically by using UIImageView & ScrollView .

Then I set different type of annotation like sign , text etc. When I finalised it to images and convert them to PDF with good result. I get different results as shown in below images.

Screen 1: convert original image size to UIImageView Size with aspect to fit ( because i have look like this type of screen) when i have convert original size to screen size at that time i have stored image original size and its used to when i have convert images to pdf after add annotation looking in screen 2.

在此输入图像描述

Screen 2. when i have finalise or convert to pdf. result look in screen 2, change annotation or sign position.

在此输入图像描述

let drawImage = jotViewController.renderImage() //sign image
signImg = drawImage
gestureView = UIView(frame: CGRect(x: 50, y: 300, width: 200, height: 110))

counter = counter + 1
gestureView.tag = counter

annotationImg = UIImageView(frame: CGRect(x: 0, y: 0, width:170, height: 80))
annotationImg.image = signImg
annotationImg.layer.cornerRadius = 5.0
// gestureView.frame = annotationImg.frame

cancelBtn.frame = CGRect(x: (annotationImg.frame.minX-10), y: (annotationImg.frame.maxY-10), width: 20, height: 20)
cancelBtn.setImage(#imageLiteral(resourceName: "cancel.png"), for: .normal)
cancelBtn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
gestureView.addSubview(cancelBtn)


zoomBtn.frame = CGRect(x: (annotationImg.frame.width-10), y: (annotationImg.frame.height-10), width: 20, height: 20)
zoomBtn.setImage(#imageLiteral(resourceName: "zoom.png"), for: .normal)
gestureView.addSubview(zoomBtn)

let zoomBtnpanGesture: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.zoomBtnPanGesture(_:)))
zoomBtnpanGesture.minimumNumberOfTouches = 1
zoomBtnpanGesture.maximumNumberOfTouches = 1
zoomBtn.addGestureRecognizer(zoomBtnpanGesture)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture))
panGesture.minimumNumberOfTouches = 1
panGesture.maximumNumberOfTouches = 1
annotationImg.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)

gestureView.addGestureRecognizer(panGesture)
annotationImg.clipsToBounds = true

gestureView.addSubview(annotationImg)

for getUIImageView in pdfUIImageViewArr {
    if tag==0 {
        let alert = UIAlertController(title: "Alert!!", message: "Please Select Signature Page.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }

    if getUIImageView.tag == tag && tag != 0 {
        getUIImageView.addSubview(gestureView)


    }
}

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(gestureViewTapped(sender:)))
tapGesture.numberOfTapsRequired = 1
gestureView.addGestureRecognizer(tapGesture)

imageViewArr.append(annotationImg)
gestureViewArr.append(gestureView)
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

@IBAction func storeAsPdfAction(_ sender: AnyObject) {
    self.storeToDocumentDir()

    let userObj = self.storyboard?.instantiateViewController(withIdentifier: "DocumentVC") as? DocumentVC
    self.navigationController?.pushViewController(userObj!, animated: true)
}

func storeToDocumentDir(){
    print(pdfImage.frame)
    print(view.frame)
    print(gestureView.frame)
    for getTextViewLbl in textViewLabelArr{
        getTextViewLbl.backgroundColor = UIColor.clear
    }

    gestureViewArr.removeFirst()
    for gestureView in gestureViewArr {

        print(gestureView.frame)

        zoomBtn.isHidden = true
        cancelBtn.isHidden = true

        //  pdfImage.addSubview(gestureView)
    }


    pdfUIImageViewArr.remove(at: 0)
    var i = 1

    for getPDFImage in pdfUIImageViewArr {
        getPDFImage.frame = CGRect(x: 0, y: 0, width: pdfImage.frame.size.width, height: pdfImage.frame.size.height)

        getPDFImage.frame = CGRect(x: 0, y: 0, width: pageRect.size.width, height: pageRect.size.height)
        getPDFImage.contentMode = UIViewContentMode.scaleToFill
        //
        let getImage = self.imageWithView(getPDFImage)

        let fileManager = FileManager.default
        let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("img\(i).jpg")
        i += 1
        let image = getImage
        print(paths)

        let imageData = UIImageJPEGRepresentation(image, 0.5)
        fileManager.createFile(atPath: paths as String, contents: imageData, attributes: nil)

        setPdfImgArr.append(getImage)
    }

    self.createPdfFromView(pdfImage, imageArrAdd: setPdfImgArr, saveToDocumentsWithFileName: "\((setFileName)!)")

    let pdfUrl = URL(fileURLWithPath:documentsFileName)
    self.setImagesInScrollView(pdfUrl)
}

Problem Solved : By Using UIScrollView Zoom:

 override func viewWillLayoutSubviews() {
    self.configureZoomScale()
}
func configureZoomScale() {
    var xZoomScale: CGFloat = self.scrollView.bounds.size.width / self.pdfImage.bounds.size.width
    var yZoomScale: CGFloat = self.scrollView.bounds.size.height / self.pdfImage.bounds.size.height
    var minZoomScale: CGFloat = min(xZoomScale, yZoomScale)
    //set minimumZoomScale to the minimum zoom scale we calculated
    //this mean that the image cant me smaller than full screen
    self.scrollView.minimumZoomScale = minZoomScale
    //allow up to 4x zoom
    self.scrollView.maximumZoomScale = 4
    //set the starting zoom scale
    self.scrollView.zoomScale = minZoomScale
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?{

    return pdfFinalImage
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM