Skip to content

Commit 6c90c63

Browse files
committed
fix(ios): replace deprecated graphics APIs
1 parent e35c302 commit 6c90c63

1 file changed

Lines changed: 46 additions & 51 deletions

File tree

ios/PdfView.swift

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -224,66 +224,61 @@ public class PdfView: UIView {
224224
return
225225
}
226226

227-
UIGraphicsBeginImageContextWithOptions(currentFrame.size, true, 0.0)
228-
guard let context = UIGraphicsGetCurrentContext() else {
229-
UIGraphicsEndImageContext()
230-
self.dispatchOnError(message: "Failed to open graphics context for rendering '\(self.source)'.")
231-
return
232-
}
233-
context.saveGState()
227+
var pageHeight: CGFloat = -1;
228+
var pageWidth: CGFloat = -1;
229+
let format = UIGraphicsImageRendererFormat()
230+
format.opaque = true
231+
let rendered = UIGraphicsImageRenderer(size: currentFrame.size, format: format).image { (uiCtx) in
232+
let context = uiCtx.cgContext
233+
context.saveGState()
234234

235-
// Default color for opaque context is black, so fill with white.
236-
UIColor.white.setFill()
237-
context.fill(currentFrame)
235+
// Default color for opaque context is black, so fill with white.
236+
UIColor.white.setFill()
237+
context.fill(CGRect(origin: CGPoint(), size: currentFrame.size))
238238

239-
let pageBounds = pdfPage.getBoxRect(.cropBox)
240-
let pageHeight: CGFloat;
241-
let pageWidth: CGFloat;
242-
if pdfPage.rotationAngle % 180 == 90 {
243-
pageHeight = pageBounds.width
244-
pageWidth = pageBounds.height
245-
} else {
246-
pageHeight = pageBounds.height
247-
pageWidth = pageBounds.width
248-
}
249-
// Change context coordinate system to pdf coordinates.
250-
let targetHeight = currentFrame.width * pageHeight / pageWidth
251-
if self.resizeMode == ResizeMode.CONTAIN {
252-
// Shift/resize so render is contained and centered in the context.
253-
if targetHeight > currentFrame.height {
254-
let targetWidth = currentFrame.height * pageWidth / pageHeight
255-
context.translateBy(x: (currentFrame.width - targetWidth) / 2, y: 0.0)
256-
let scaleFactor = currentFrame.height / targetHeight
257-
context.scaleBy(x: scaleFactor, y: scaleFactor)
239+
let pageBounds = pdfPage.getBoxRect(.cropBox)
240+
if pdfPage.rotationAngle % 180 == 90 {
241+
pageHeight = pageBounds.width
242+
pageWidth = pageBounds.height
258243
} else {
259-
context.translateBy(x: 0.0, y: (currentFrame.height - targetHeight) / 2)
244+
pageHeight = pageBounds.height
245+
pageWidth = pageBounds.width
260246
}
261-
}
262-
context.translateBy(x: 0.0, y: targetHeight)
263-
context.scaleBy(x: currentFrame.width / pageWidth, y: -targetHeight / pageHeight)
264-
context.concatenate(pdfPage.getDrawingTransform(
265-
.cropBox,
266-
rect: CGRect(x: 0.0, y: 0.0, width: pageWidth, height: pageHeight),
267-
rotate: 0,
268-
preserveAspectRatio: false
269-
))
270-
271-
context.interpolationQuality = .high
272-
context.setRenderingIntent(.defaultIntent)
273-
context.drawPDFPage(pdfPage)
274-
context.restoreGState()
275-
276-
context.saveGState()
277-
self.renderAnnotation(context, scaleX: currentFrame.width, scaleY: currentFrame.height)
278-
context.restoreGState()
247+
// Change context coordinate system to pdf coordinates.
248+
let targetHeight = currentFrame.width * pageHeight / pageWidth
249+
if self.resizeMode == ResizeMode.CONTAIN {
250+
// Shift/resize so render is contained and centered in the context.
251+
if targetHeight > currentFrame.height {
252+
let targetWidth = currentFrame.height * pageWidth / pageHeight
253+
context.translateBy(x: (currentFrame.width - targetWidth) / 2, y: 0.0)
254+
let scaleFactor = currentFrame.height / targetHeight
255+
context.scaleBy(x: scaleFactor, y: scaleFactor)
256+
} else {
257+
context.translateBy(x: 0.0, y: (currentFrame.height - targetHeight) / 2)
258+
}
259+
}
260+
context.translateBy(x: 0.0, y: targetHeight)
261+
context.scaleBy(x: currentFrame.width / pageWidth, y: -targetHeight / pageHeight)
262+
context.concatenate(pdfPage.getDrawingTransform(
263+
.cropBox,
264+
rect: CGRect(x: 0.0, y: 0.0, width: pageWidth, height: pageHeight),
265+
rotate: 0,
266+
preserveAspectRatio: false
267+
))
279268

280-
let rendered = UIGraphicsGetImageFromCurrentImageContext()
269+
context.interpolationQuality = .high
270+
context.setRenderingIntent(.defaultIntent)
271+
context.drawPDFPage(pdfPage)
272+
context.restoreGState()
281273

282-
UIGraphicsEndImageContext()
274+
context.saveGState()
275+
self.renderAnnotation(context, scaleX: currentFrame.width, scaleY: currentFrame.height)
276+
context.restoreGState()
277+
}
283278

284279
DispatchQueue.main.async {
285280
// Post new bitmap for display.
286-
self.layer.contents = rendered?.cgImage
281+
self.layer.contents = rendered.cgImage
287282
}
288283
self.dispatchOnLoadComplete(pageWidth: pageWidth, pageHeight: pageHeight)
289284
}

0 commit comments

Comments
 (0)