Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load PDF into UIImage or UIImageView?

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.

like image 364
Peter Hajas Avatar asked Mar 01 '10 21:03

Peter Hajas


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is use of UIImageView?

Overview. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.

What is UIImageView?

An object that manages image data in your app.


5 Answers

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.

like image 176
LightMan Avatar answered Oct 15 '22 13:10

LightMan


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

like image 38
Hua-Ying Avatar answered Oct 15 '22 12:10

Hua-Ying


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

like image 31
Tony Lenzi Avatar answered Oct 15 '22 13:10

Tony Lenzi


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
like image 37
RvdH Avatar answered Oct 15 '22 11:10

RvdH


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

like image 36
Chanaka Anuradh Caldera Avatar answered Oct 15 '22 12:10

Chanaka Anuradh Caldera