Swift 游乐场中的 PDF 生成
PDF Generation in Swift Playground
是否可以使用 swift playground 创建 PDF 文件?
我正在编写一个应用程序,它可以自动从一组选定的对象中生成一个对象,但是检查我所做的每个更改的过程很快就会变得很痛苦。
我不确定 Playground 在文档目录或其他方面的工作方式,因为我只将它用于文档。
可以这样做吗?
编辑:
所以我设法通过使用目录 NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
从 playground 生成一个 PDF
我已将 Web 视图放入 playground 中尝试预览,但没有结果。 playground 中还有其他实时方式吗?
Web 视图正在使用正确的 URL 加载请求,因为我使用相同的 url 在 Finder 中导航到它,但它只显示一个空白页面正在 Playground 中预览,尽管出现了图像在 Finder 的 PDF 中
游乐场文件
import UIKit
// Properties
var documentDirectories : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentDirectory : String = documentDirectories[0] as! String
var pdfFileName : String = "bill.pdf"
var pdfPathWithFileName : String = (documentDirectory as NSString).stringByAppendingPathComponent(pdfFileName)
let pageSize : CGSize = CGSize(width: 200, height: 1000)
// Graphics Context
UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil)
UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: 850, height: 1000), nil)
let context : CGContextRef = UIGraphicsGetCurrentContext()!
let rect : CGRect = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
// Fill background
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, rect)
// Products
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
for var i = 0; i < 10; i++ {
let rect : CGRect = CGRect(x: 100, y: (i * 50), width: 300, height: 50)
let productName : NSString = "ProductNo\(i)"
productName.drawWithRect(rect, options: .UsesFontLeading, attributes: nil, context: NSStringDrawingContext.init())
let imageRect : CGRect = CGRect(x: 20, y: (i*50), width: 50, height: 50)
let image = UIImage(named: "testImage.png")
image?.drawInRect(imageRect)
}
// End PDF Context
UIGraphicsEndPDFContext()
// PDF Web Request
let pdfURL = NSURL(string: pdfPathWithFileName)
let request = NSURLRequest(URL: pdfURL!)
// Web View
let webSize = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
let webView = UIWebView(frame: webSize)
webView.loadRequest(request)
let scroll : UIScrollView = webView.scrollView
let zoom = webView.bounds.size.width/scroll.contentSize.width
scroll.setZoomScale(zoom, animated: true)
如果激活菜单:
View > Debug Area > Show Debug Area
然后在控制台中可以看到:
Failed to obtain sandbox extension for...
Playgrounds 是沙盒化的,您不能使用它们来操作 Playground 自身资源之外的文件,NSSearchPathForDirectoriesInDomains
在这种情况下毫无用处。
图像(或您要使用的任何文件)必须位于 Playground 的 Resources
文件夹中,以便您能够通过 UIImage(named:)
或 [=13] 等方法使用它们=].
如果激活菜单:
View > Navigators > Show Project Navigator
您将能够将图像和文件添加到 Playground 的 Resources
文件夹中。
注意:这个 Resources 文件夹实际上是一个 link 到 Playground 自动生成的唯一缓存文件夹中的一个文件夹。你不应该尝试直接使用它的 URL 而是通过 NSBundle(或其他)。
我不确定这是否是您想要的,但以下是我将 PDF 文件导入 Swift Playground for iPad 的方法:
首先,进行 Image
扩展(灵感来自 Paul Hudson 的 post:How to render a PDF to an image):
import SwiftUI
// Image(pdf: url)
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)
}
}
然后将 PDF 文件导入 Playground(它适用于我的 iPad,如果你想在 Xcode 上工作,你必须自己尝试):
然后直接使用扩展名,例如:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
Image(pdf: #fileLiteral(resourceName: "Card1.pdf"))
Image(pdf: #fileLiteral(resourceName: "Card3.pdf"))
}// container
.padding()
.shadow(radius: 8)
.background(Color.gray)
.cornerRadius(10)
}
}
PlaygroundPage.current.setLiveView(ContentView())
这个#fileLiteral
东西藏在游乐场上,它实际上是这样的:
虽然看起来很奇怪,但它确实有效,结果如下所示:
希望对您有所帮助。
是否可以使用 swift playground 创建 PDF 文件?
我正在编写一个应用程序,它可以自动从一组选定的对象中生成一个对象,但是检查我所做的每个更改的过程很快就会变得很痛苦。
我不确定 Playground 在文档目录或其他方面的工作方式,因为我只将它用于文档。
可以这样做吗?
编辑:
所以我设法通过使用目录 NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
我已将 Web 视图放入 playground 中尝试预览,但没有结果。 playground 中还有其他实时方式吗?
Web 视图正在使用正确的 URL 加载请求,因为我使用相同的 url 在 Finder 中导航到它,但它只显示一个空白页面正在 Playground 中预览,尽管出现了图像在 Finder 的 PDF 中
游乐场文件
import UIKit
// Properties
var documentDirectories : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentDirectory : String = documentDirectories[0] as! String
var pdfFileName : String = "bill.pdf"
var pdfPathWithFileName : String = (documentDirectory as NSString).stringByAppendingPathComponent(pdfFileName)
let pageSize : CGSize = CGSize(width: 200, height: 1000)
// Graphics Context
UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil)
UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: 850, height: 1000), nil)
let context : CGContextRef = UIGraphicsGetCurrentContext()!
let rect : CGRect = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
// Fill background
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, rect)
// Products
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
for var i = 0; i < 10; i++ {
let rect : CGRect = CGRect(x: 100, y: (i * 50), width: 300, height: 50)
let productName : NSString = "ProductNo\(i)"
productName.drawWithRect(rect, options: .UsesFontLeading, attributes: nil, context: NSStringDrawingContext.init())
let imageRect : CGRect = CGRect(x: 20, y: (i*50), width: 50, height: 50)
let image = UIImage(named: "testImage.png")
image?.drawInRect(imageRect)
}
// End PDF Context
UIGraphicsEndPDFContext()
// PDF Web Request
let pdfURL = NSURL(string: pdfPathWithFileName)
let request = NSURLRequest(URL: pdfURL!)
// Web View
let webSize = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
let webView = UIWebView(frame: webSize)
webView.loadRequest(request)
let scroll : UIScrollView = webView.scrollView
let zoom = webView.bounds.size.width/scroll.contentSize.width
scroll.setZoomScale(zoom, animated: true)
如果激活菜单:
View > Debug Area > Show Debug Area
然后在控制台中可以看到:
Failed to obtain sandbox extension for...
Playgrounds 是沙盒化的,您不能使用它们来操作 Playground 自身资源之外的文件,NSSearchPathForDirectoriesInDomains
在这种情况下毫无用处。
图像(或您要使用的任何文件)必须位于 Playground 的 Resources
文件夹中,以便您能够通过 UIImage(named:)
或 [=13] 等方法使用它们=].
如果激活菜单:
View > Navigators > Show Project Navigator
您将能够将图像和文件添加到 Playground 的 Resources
文件夹中。
注意:这个 Resources 文件夹实际上是一个 link 到 Playground 自动生成的唯一缓存文件夹中的一个文件夹。你不应该尝试直接使用它的 URL 而是通过 NSBundle(或其他)。
我不确定这是否是您想要的,但以下是我将 PDF 文件导入 Swift Playground for iPad 的方法:
首先,进行 Image
扩展(灵感来自 Paul Hudson 的 post:How to render a PDF to an image):
import SwiftUI
// Image(pdf: url)
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)
}
}
然后将 PDF 文件导入 Playground(它适用于我的 iPad,如果你想在 Xcode 上工作,你必须自己尝试):
然后直接使用扩展名,例如:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
Image(pdf: #fileLiteral(resourceName: "Card1.pdf"))
Image(pdf: #fileLiteral(resourceName: "Card3.pdf"))
}// container
.padding()
.shadow(radius: 8)
.background(Color.gray)
.cornerRadius(10)
}
}
PlaygroundPage.current.setLiveView(ContentView())
这个#fileLiteral
东西藏在游乐场上,它实际上是这样的:
虽然看起来很奇怪,但它确实有效,结果如下所示:
希望对您有所帮助。