Post

Replies

Boosts

Views

Activity

Reply to Playground with PDF
First, make an Image extension: 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: 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())
Oct ’20