swift - Swift Playground 中的 PDF 生成

标签 swift pdf-generation swift-playground

是否可以使用 swift playground 创建 PDF 文件?
我正在编写一个应用程序,它可以自动从一组选定的对象中生成一个对象,但是检查我所做的每个更改的过程很快就会变得很痛苦。

我不确定 Playground 在文档目录或其他方面的工作方式,因为我只将它用于文档。

这有可能吗?

编辑:

因此,我已经设法通过使用目录 NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

从 playground 生成一个 PDF

我已将 WebView 放入 playground 以尝试预览它但没有结果。在 Playground 上还有其他实时方式吗?

Web View 正在使用正确的 URL 加载请求,因为我使用相同的 url 在 Finder 中导航到它,但它只显示一个空白页面正在 Playground 中预览,尽管图像出现在 Finder 的 PDF 中

Playground 文件

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:) 等方法使用它们NSBundle.mainBundle().URLForResource

如果您激活菜单:

View > Navigators > Show Project Navigator

您可以将图像和文件添加到 Playground 的 Resources 文件夹中。

注意:此 Resources 文件夹实际上是指向 Playground 自动生成的唯一缓存文件夹中的文件夹的链接。您不应该尝试直接使用它的 URL,而是通过 NSBundle(或其他方式)。

关于swift - Swift Playground 中的 PDF 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461774/

相关文章:

objective-c - ios - uiscrollview 中的 pdf 生成

python - 报告实验室布局错误 : too large on page

javascript - jsPDF autotable右对齐x位置错误

swift - 代码在 Playground 上运行良好;同时它产生一个 EXC_I386_GPFLT 错误

ios - 我的 UITableView 中没有显示任何数据

ios - 从 navigationItem 中删除项目以编程方式显示 navigationItems 标题

ios - UINavigationBar 不在 Playground 中显示项目部件

ios - 在 iOS Playground 上使用 AVFoundation 的 Recorder,但 recorder.record() 总是返回 false

ios - 向 WKWebView 添加加载栏并选择其颜色

ios - 访问核心数据中的子对象时出现关系错误