8

This is my first ever question on stackoverflow.

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.

Thanks so much for the help!

Peter Hajas
  • 3,861
  • 4
  • 23
  • 27

6 Answers6

9

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.

LightMan
  • 3,068
  • 26
  • 26
  • 7
    it is not rendering a pdf. The Asset catalog breaks out the pdf to pngs for each scale size at build time. – maxpower Mar 14 '16 at 21:58
  • @maxpower 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 answer – OhadM Mar 22 '18 at 16:51
  • 1
    Things are a bit different for iOS11+ https://useyourloaf.com/blog/xcode-9-vector-images/ – maxpower Mar 27 '18 at 02:22
6

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

Hua-Ying
  • 3,166
  • 4
  • 21
  • 25
  • Are there any built in functions to translate a PDF document into a PNG? – Peter Hajas Mar 01 '10 at 22:29
  • 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. – Hua-Ying Mar 02 '10 at 15:04
6

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

Tony Lenzi
  • 4,199
  • 4
  • 29
  • 25
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
RvdH
  • 72
  • 6
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

Chanaka Anuradh Caldera
  • 4,280
  • 7
  • 34
  • 61
0

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