将 PDF 加载到 UIImage 或 UIImageView 中?

发布于 2024-08-23 13:53:04 字数 137 浏览 3 评论 0原文

我正在尝试将资源中存储的 PDF 文件加载到 iPhone SDK 中的 UIImage/UIImageView 对象中。如果不将 PDF 转换为图像,这是否可行?如果没有,将 PDF 转换为 PNG 的成员函数是什么?如果可能的话,我真的更愿意保留 PDF。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雄赳赳气昂昂 2024-08-30 13:53:04

现在可以了,我使用的是 iOS 8+。
如果您将 pdf 放入资产目录中,然后使用 [UIImage imageNamed:] 将 pdf 加载到 UIImage 中,则会将 pdf 的第一页渲染到 UIImage 中。

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.

一杯敬自由 2024-08-30 13:53:04

PDF 可以通过 UIWebView 加载。如果您需要更多控制,可以使用 Quartz 2D 渲染 PDF: Quartz2D

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

吖咩 2024-08-30 13:53:04

您可能想在 GitHub 上查看此 UIImage-PDF 类别:https://github.com/mindbrix/ UIImage-PDF

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

忘羡 2024-08-30 13:53:04

基于 '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

用法:

let img = drawIMGfromPDF(thisPDF: "fsm75-3 (Startstrecke)") //Bild vom PDF erstellen
cell.outletImageView.image = img

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
凌乱心跳 2024-08-30 13:53:04

对于 Swift

只需导入添加您的PDF资产并像使用它一样
如下。

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

就是这样,这应该很有效

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

横笛休吹塞上声 2024-08-30 13:53:04

对于斯威夫特来说,
只需将 PDF 导入或添加到资产并按如下方式使用即可。

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

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")
叹沉浮 2024-08-30 13:53:04

您可以使用代码打击。该代码与其他答案类似。诀窍是 UIColor.clear.set() 而不是 UIColor.white.set() 行。如果使用白色背景将是白色的。

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
}

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
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文