注释(图像和文本)子视图在保存后更改位置

时间:2016-12-02 10:58:54

标签: ios swift uiimageview cgpdfdocument

我创建了一个基于PDF的应用程序。首先,我从文档目录中获取PDF,然后将其转换为图像,然后使用UIImageView& amp;编程将所有图像设置为垂直。 ScrollView

然后我设置了不同类型的注释,例如signtext等。当我将其最终化为图像并将其转换为PDF并获得良好结果时。我得到了不同的结果,如下图所示。

屏幕1:将原始图像尺寸转换为UIImageView尺寸,方面适合(因为我看起来像这种类型的屏幕)当我将原始尺寸转换为屏幕尺寸时我已存储图像原始大小及其用于在添加注释后在屏幕2中将图像转换为pdf时。

enter image description here

屏幕2。,当我完成或转换为pdf时。结果在屏幕2中查看,更改注释或签名位置。

enter image description here

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)
}

0 个答案:

没有答案