如何在 Swift 中从 .writetoFile 恢复 PDF's [英] How to recover PDF's from .writetoFile in Swift

查看:25
本文介绍了如何在 Swift 中从 .writetoFile 恢复 PDF's的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .writetofile 保存图像,但我不知道如何恢复它.这是我保存图像的方式:

I'm saving an image using .writetofile but I don't know how to recover it. This how I save the image:

self.pdfData.writeToURL(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent("testgogo.pdf"), atomically: true) // what it is saved as


        self.pdfData.writeToFile("tessst.pdf", atomically: false)
        print(NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!.path!)


        var pdfData: NSData {
            let result = NSMutableData()
            UIGraphicsBeginPDFContextToData(result, frame, nil)
            guard let context = UIGraphicsGetCurrentContext()
  else { return result }

     UIGraphicsBeginPDFPage()
     layer.renderInContext(context)
     UIGraphicsEndPDFContext()
     return result
}

我以后如何取回图像?

推荐答案

这里有一个示例,说明如何在 Swift 2.x 中执行此操作.

Here is an example of how you could do it in Swift 2.x.

它使用 NSData(contentsOfFile: myFilePath) 来加载文件.该示例使用 PNG 文件.

It uses the NSData(contentsOfFile: myFilePath) to load the file. The example uses a PNG file.

直接从我的游乐场:

import UIKit

/*
 * Creates an UIImage from a UIView
 */
func createImage(fromView view: UIView) -> UIImage {
    UIGraphicsBeginImageContext(view.frame.size)
    let context = UIGraphicsGetCurrentContext()
    view.layer.renderInContext(context!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    return image
}

/*
 * Finds the path in Document folder
 */
func createMyFilePath(forFileName fileName: String) -> String? {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)
    if let path = paths.first {
        return path + "/\(fileName)"
    }
    return nil
}

/*
 * Main behaviour
 */

// ImageView with Label on it
let imageView = UIImageView(image: UIImage(named: "borat"))
let label = UILabel(frame: imageView.frame)
label.font = UIFont(name: "helvetica", size: 40)
label.text = "Great Success!"
imageView .addSubview(label)

// Find the path where to save
guard let myFilePath = createMyFilePath(forFileName: "borat-with-label.png") else {
    print("Cannot generate file path ☹️")
    exit(0)
}

// Use this to check in finder where your file is saved
print(myFilePath)

// Transform the imageView in UIImage to save it
let imageToSave = createImage(fromView: imageView)

// Get the image as data
guard let imageToSaveAsData = UIImagePNGRepresentation(imageToSave) else {
    print("Cannot transform image to data ☹️")
    exit(1)
}

// Save to Disk!
do{
    try imageToSaveAsData.writeToFile(myFilePath, options: .DataWritingAtomic)
} catch {
    print("Error, cannot write to the location \(myFilePath)")
}

// Load from Disk!
let loadedImageData = NSData(contentsOfFile: myFilePath)

// Check the data is the same
if loadedImageData == imageToSaveAsData {
    print("✌️")
}

// Have a look at the loaded image!
UIImage(data: loadedImageData!)

这篇关于如何在 Swift 中从 .writetoFile 恢复 PDF's的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆