Playground with PDF

Hello!


I'm creating a project to quickly create views in my project. But for some reason, when I try to create an image with a pdf resource it's not working. Playgrounds and PDF resources are not compatible?


any clue?


thanks in advance!

Accepted Reply

How are you creating the image? iOS’s PDF support is a lot more limited than macOS’s. So, for example, if you create an iOS app and add

test.pdf
resource to it, code like this:
let pdfURL = Bundle.main.url(forResource: "test", withExtension: "pdf")!
let image = UIImage(contentsOfFile: pdfURL.path)
print(image)

prints

nil
because UIImage doesn’t support reading from PDF directly.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Are you working on an iOS or macOS playground?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm working on iOS playground

How are you creating the image? iOS’s PDF support is a lot more limited than macOS’s. So, for example, if you create an iOS app and add

test.pdf
resource to it, code like this:
let pdfURL = Bundle.main.url(forResource: "test", withExtension: "pdf")!
let image = UIImage(contentsOfFile: pdfURL.path)
print(image)

prints

nil
because UIImage doesn’t support reading from PDF directly.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

So, I can't create images with PDF resources in playgrounds. For instance, just as you said, It always will return nil

resources folder -> icon.pdf

let image = UIImage(named: "icon") // nil


thanks for your time!

First, make an Image extension:
Code Block swift
import SwiftUI
extension Image {
public init?(pdf url: URL) {
guard
let doc = CGPDFDocument(url as CFURL),
let page = doc .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)
}
self = Image(uiImage: img)
}
}


Second, import your PDF into Swift Playgrounds (it works on my iPad, you have to try it yourself on Xcode) and use the extension:

Code Block swift
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
Image(pdf: #fileLiteral(resourceName: "Card1.pdf"))
Image(pdf: #fileLiteral(resourceName: "Card3.pdf"))
}
.padding()
.shadow(radius: 8)
.background(Color.gray)
.cornerRadius(10)
}
}
PlaygroundPage.current.setLiveView(ContentView())


  • Use Xcode on Mac to open the Playground project, then create the xcassets file
  • Add your assets to the xcassets
  • Open the Playground file from Playground

then you should be able to use the image and see the Assets section