私は現在アプリケーションを開発中で、UICollectionViewの上にPDFリーダーを実装したいと思います。CGPDFPage図面がメモリの問題を引き起こす(Swift 3)
私は、対応するPDFのページをレンダリングし、各セルにカスタムUIViewのを使用しています:
weak var page: CGPDFPage! {
didSet { setNeedsDisplay() }
}
override func draw(_ rect: CGRect) {
if page == nil {
print("page is nil")
return }
let context = UIGraphicsGetCurrentContext()
context?.clear(bounds)
context?.setFillColor(red: 1, green: 1, blue: 1, alpha: 1)
context?.fill(bounds)
context?.translateBy(x: 0.0, y: bounds.size.height);
context?.scaleBy(x: 1.0, y: -1.0);
var cropBox = page.getBoxRect(.cropBox)
cropBox = CGRect(x: cropBox.origin.x, y: cropBox.origin.y, width: ceil(cropBox.width), height: ceil(cropBox.height))
let scaleFactor = min(bounds.size.width/cropBox.size.width, bounds.size.height/cropBox.size.height)
let scale = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
let scaledInnerRect = cropBox.applying(scale)
let translate = CGAffineTransform(translationX: ((bounds.size.width - scaledInnerRect.size.width)/2) - scaledInnerRect.origin.x, y: ((bounds.size.height - scaledInnerRect.size.height)/2) - scaledInnerRect.origin.y)
let concat = translate.scaledBy(x: scaleFactor, y: scaleFactor)
context?.concatenate(concat)
let clipRect = cropBox
context?.addRect(clipRect)
context?.clip()
context?.drawPDFPage(page)
UIGraphicsEndPDFContext()
}
をこれまでのところは良いです。ページをセルにレンダリングしますが、メモリの問題が発生します。
どういうわけか、コンテキストは、コレクションビューのセルにレンダリングすべてのページへの参照を保持し、彼らが割り当て解除取得されていません。 CGPDFPageReleaseは使用できなくなりました。 :(一方
を、このUIViewのサブクラスは、私が緩んで品質をしないように、私はPDFファイルを再描画する方法scrollviewの内側にズームされた場合?
すべてのヘルプは高く評価されます。ありがとうございます!
ビューのサブクラスで下記のメソッドをオーバーライドし、それは素晴らしい仕事 –