objective-c - 如何在 Swift 中从 .writetoFile 恢复 PDF

标签 objective-c xcode swift writetofile

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

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 中执行此操作的示例。

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

直接来 self 的 Playground:

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!)

关于objective-c - 如何在 Swift 中从 .writetoFile 恢复 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35945469/

相关文章:

objective-c - 是否可以自定义 CNContactPickerViewController?

ios - 获取特定的小数位

ios - 删除字符串开头和结尾的空格

ios - 如何从此 NSString 获取日期(不是时间或任何字母字符)?

objective-c - NSImageView - 巨大的内存泄漏?

ios - 尝试了解 iOS 8 中的 UIScrollView 自动布局

ios - 使用 avplayer 长缓冲流式传输音频 - playImmediatelyAtRate 不起作用

objective-c - ARC 中的假 va_list

ios - 删除部分时 UICollectionView 崩溃

xcode - 我不小心删除了 xcode 项目文件,但我在 itunes 上连接了 Xcode 7.3