前提・実現したいこと
storyboardで配置したUIImageViewをPDF化し
PDFのサイズをさらに大きいサイズに変更すると
画質が落ちてしまいます。
どうにかして画質を落とさずにPDFにしたいです。
画質が落ちてしまう原因は、"PDFにしてから拡大する"ためではないかと思ったので
PDFにする前のUIImageViewのサイズを大きくしようとしたのですが上手くいきませんでした。
ちなみに、大きくしたUIImageViewは画面に表示させないようにしたいです。
ご教授よろしくお願いいたします。
該当のソースコード
swift
1import UIKit 2 3 4class ViewController2: UIViewController, UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate { 5 6 let image1: UIImage = UIImage(named:"image1-1.jpg")! 7 @IBOutlet weak var getimage: UIImageView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 // Screen Size の取得 13 screenWidth = self.view.bounds.width 14 screenHeight = self.view.bounds.height 15 16 let imageHeight = screenWidth * 210 / 148 17 let imagerect:CGRect = CGRect(x:0, y:0, width:screenWidth, height:imageHeight) 18 19 20 // ImageView frame をCGRectで作った矩形に合わせる 21 getimage.frame = imagerect; 22 23 // 画像の中心をスクリーンの中心位置に設定 24 getimage.center = CGPoint(x:screenWidth/2, y:screenHeigh/2) 25 26 27 // view に ImageView を追加する 28 self.view.addSubview(getimage) 29 30 getimage.image = bin1 31} 32 33
PDFにする
swift
1//PDFを表示させるため次の画面へ遷移 2 @IBAction func push(_ sender: Any) { 3 print("push") 4 5 6 let x = getimage.frame.origin.x 7 let y = getimage.frame.origin.y 8 UserDefaults.standard.set(y, forKey: "uekaranokyori") 9 10 11//画像の解像度あげたい 12 let imageHeight = screenWidth * 210 / 148 13 14 let getimagerect:CGRect = CGRect(x:x, y:y, width:screenWidth*3, height:imageHeight*3) 15 getimage.frame = getimagerect; 16 17 18 19 //UIImageView(getimage)のサイズ取得 20 let imageWidth2 = self.getimage.frame.width 21 let imageHeight2 = self.getimage.frame.height 22 23 24 let pdfData = NSMutableData() 25 //どんな大きさ、位置のものを表示するのか 26 UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: x, y: -y, width: imageWidth2, height: imageHeight2), nil) 27 UIGraphicsBeginPDFPage() 28 29 guard let pdfContext = UIGraphicsGetCurrentContext() else { return } 30 self.view.layer.render(in: pdfContext) 31 UIGraphicsEndPDFContext() 32 33 if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first { 34 let documentsFileName = documentDirectories + "/test.pdf" 35 //パスを保存して次の画面で表示させる 36 UserDefaults.standard.set(documentsFileName, forKey:"filePath") 37 debugPrint(documentsFileName) 38 pdfData.write(toFile: documentsFileName, atomically: true) 39 } 40 41 } 42 43//この画面に戻ってきても拡大したUIImageViewが見えないようにサイズを元にもどす 44 let getimagerectback:CGRect = CGRect(x:x, y:y, width:screenWidth, height:imageHeight) 45 getimage.frame = getimagerectback; 46
補足情報(FW/ツールのバージョンなど)
Swiftのversionは4.1.2
Xcodeのversionは9.4.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/02 04:56
2019/02/03 15:40
2019/02/03 18:48 編集