Load PDF into UIImage or UIImageView?
Asked Answered
B

7

11

I'm trying to load a PDF file stored in Resources into a UIImage/UIImageView object in the iPhone SDK. Is this at all possible without converting the PDF to an image? If not, what are the member functions for converting a PDF into a PNG? I would really prefer to preserve the PDF if at all pssoible.

Baloney answered 1/3, 2010 at 21:25 Comment(0)
P
6

PDFs can be loaded by UIWebView. If you need more control you can use Quartz 2D to render PDFs: Quartz2D

Picador answered 1/3, 2010 at 21:51 Comment(2)
Are there any built in functions to translate a PDF document into a PNG?Baloney
I haven't tried it but you could try using quartz 2d to CGImageRef then create a UIImage with that ref then try using UIImagePNGRepresentation. Again, never tried it.Picador
P
11

Now it is possible, I am using iOS 8+. If you put a pdf in your assets catalog, and then load the pdf in a UIImage using [UIImage imageNamed:] it renders the first page of the pdf into the UIImage.

Perlaperle answered 27/10, 2015 at 8:8 Comment(3)
it is not rendering a pdf. The Asset catalog breaks out the pdf to pngs for each scale size at build time.Wellnigh
@Wellnigh is correct, during build time xcode breaks the pdf vector into relevant images. If you have a pdf file that is inside the document folder and NOT the asset catalog you may want to use Tony Lenzi's answerKnob
Things are a bit different for iOS11+ useyourloaf.com/blog/xcode-9-vector-imagesWellnigh
P
6

PDFs can be loaded by UIWebView. If you need more control you can use Quartz 2D to render PDFs: Quartz2D

Picador answered 1/3, 2010 at 21:51 Comment(2)
Are there any built in functions to translate a PDF document into a PNG?Baloney
I haven't tried it but you could try using quartz 2d to CGImageRef then create a UIImage with that ref then try using UIImagePNGRepresentation. Again, never tried it.Picador
A
6

You may want to check out this UIImage-PDF category over on GitHub: https://github.com/mindbrix/UIImage-PDF

Air answered 10/2, 2012 at 18:11 Comment(0)
J
3

a solution based on 'https://www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image'

//MARK: - für Bild vom PDF erstellen
extension SettingsQuellen{
func drawIMGfromPDF(thisPDF: String) -> UIImage? {

    ///holt das PDF aus einem Verzeichnis im Programm
    ///nicht aus der 'Assets.xcassets'

    let path = Bundle.main.path(
        forResource: thisPDF,  //"fsm75-3 (Startstrecke)"
        ofType     : "pdf")  /////path to local file

    let url = URL(fileURLWithPath: path!)

    //following based on: https://www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image

    guard let document = CGPDFDocument(url as CFURL) else { return nil }
    guard let page = document.page(at: 1) else { return nil }

    let pageRect = page.getBoxRect(.mediaBox)
    let renderer = UIGraphicsImageRenderer(size: pageRect.size)
    let img = renderer.image { ctx in
        UIColor.white.set()
        ctx.fill(pageRect)

        ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
        ctx.cgContext.scaleBy(x: 1.0, y: -1.0)

        ctx.cgContext.drawPDFPage(page)
    }

    return img
}//end func drawIMGfromPDF
}//end extension SettingsQuellen - für Bild vom PDF erstellen

Usage:

let img = drawIMGfromPDF(thisPDF: "fsm75-3 (Startstrecke)") //Bild vom PDF erstellen
cell.outletImageView.image = img
Jipijapa answered 12/2, 2020 at 11:26 Comment(0)
P
2

for Swift,

just Import or Add your PDF to Assets and use it like below.

your_image_view.image = UIImage(named:"name_of_your_file")

that is it and this should work charmly

Palermo answered 16/4, 2020 at 23:55 Comment(0)
H
1

for Swift, just Import or Add your PDF to Assets and use it like below.

your_image_view.image = UIImage(named:"name_of_your_file")
Hiller answered 2/3, 2021 at 6:16 Comment(0)
M
0

You can use code blow. This code is the similar the other answers. The trick is UIColor.clear.set() instead of UIColor.white.set() line. If you use white background will be white.

func drawPDFfromURL(url: URL) - > UIImage ? {
  guard
  let document = CGPDFDocument(url as CFURL)
  else {
    return nil
  }
  guard
  let page = document.page(at: 1)
  else {
    return nil
  }

  let pageRect = page.getBoxRect(.mediaBox)
  let renderer = UIGraphicsImageRenderer(size: pageRect.size)
  let img = renderer.image {
    ctx in
      UIColor.clear.set()
    ctx.fill(pageRect)

    ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
    ctx.cgContext.scaleBy(x: 1.0, y: -1.0)

    ctx.cgContext.drawPDFPage(page)
  }

  return img
}
Multiply answered 8/1 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.