ios - 在 swift 3 中将字节数组转换为 PDF

标签 ios arrays swift swift3

我想做的是,从 api 响应中获取字节数组响应,并将其保存到文件 (PDF)。

(服务器返回一个字节数组)

正在使用 Alamofire 进行网络请求,

Alamofire.request("\(BaseUrl)api/mobile/downloadResume", headers : ProfileViewHeader)
        .response { response in
    print(response)
        // let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        // let filePath = "\(documentsPath)/readingfile.pdf"
        // let data:NSData = NSKeyedArchiver.archivedData(withRootObject: response.data) as NSData
        // let file = data.write(toFile: filePath, atomically: false)
}

我正在毫无问题地获得字节数组响应,

我的主要问题是如何将字节数组保存在文件中。我在 Android 中做了同样的事情,没有任何问题,但无法快速实现,

我们将不胜感激任何形式的帮助。请帮帮我。

谢谢

最佳答案

对于 future 的读者,这就是我使用 swiftyJson 的方式:

let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

let fileURL = documentDirectory.appendingPathComponent("TEST2.pdf")

let bytes = json["Recibo"].arrayObject as! [UInt8]

let data = Data(bytes);
data.write(to: fileURL, options: .atomic)

关于ios - 在 swift 3 中将字节数组转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44754778/

相关文章:

ios - 在 UITableView 上获取 Spotify Track 图稿

c - 如何对表示时间表的数组结构对象进行排序

在设备上运行时的 iOS 8 Beta 3 SpriteKit exc_bad_access

xcode - Collection View :didSelectItemAtIndexPath - does not get called

ios - 如何使任意 UIAccessibilityElement 的行为类似于 VoiceOver 的 UISwitch?

IOS:通过 tableView 导航并在下一个 View 中显示 TableView 数据

ios - 启用 guard malloc 时出现奇怪的错误

c++ - 错误] 无法通过 'std::string {aka class std::basic_string<char>}' 传递非平凡可复制类型 '...' 的对象

c - 如何将指向 C(而非 C++)中的结构的指针数组设为空

swift - 将图像存储在 swift 的 Core Data 中