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

Ios 在swift 3中将字节数组转换为PDF,ios,arrays,swift,swift3,Ios,Arrays,Swift,Swift3,我试图做的是,从api响应中获取字节数组响应,并将其保存到文件(PDF) (服务器返回字节数组) Am使用Alamofire进行web请求 Alamofire.request("\(BaseUrl)api/mobile/downloadResume", headers : ProfileViewHeader) .response { response in print(response) // let documentsPath = NSSearchPat

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

(服务器返回字节数组)

Am使用Alamofire进行web请求

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上也做过同样的事情,没有任何问题,但在swift上无法实现同样的效果

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


感谢使用Alamofire,结果是内部数据。您可以这样做以保存在文档目录下的文件中

   override func viewDidAppear(_ animated: Bool) {


    Alamofire.request("LINK", headers : nil)
    .response { response in
        print(response.data ?? "")


        self.createPDF(pdfData: response.data!)
    }
}


func createPDF( pdfData : Data) {
    let render = UIPrintPageRenderer()

    let html = "<b>Hello <i>World!</i></b> <p>Generate PDF file from HTML in Swift</p>"
    let fmt = UIMarkupTextPrintFormatter(markupText: html)

    render.addPrintFormatter(fmt, startingAtPageAt: 0)

    // 3. Assign paperRect and printableRect

    let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
    let printable = page.insetBy(dx: 0, dy: 0)

    render.setValue(NSValue(cgRect: page), forKey: "paperRect")
    render.setValue(NSValue(cgRect: printable), forKey: "printableRect")

    // 4. Create PDF context and draw
    //let pointzero = CGPoint(x: 0,y :0)
    let rect = CGRect.zero


    let data = NSMutableData(base64Encoded: pdfData.base64EncodedData(), options: NSData.Base64DecodingOptions.ignoreUnknownCharacters);

    UIGraphicsBeginPDFContextToData(data!, rect, nil)


    for i in 1...render.numberOfPages {

        UIGraphicsBeginPDFPage();
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPage(at: i - 1, in: bounds)
    }

    UIGraphicsEndPDFContext();

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

    // 5. Save PDF file
    do {
        let fileURL = try documentsPath.asURL().appendingPathComponent("file.pdf")
       try pdfData.write(to: fileURL, options: .atomic)

    } catch {

    }


    print("saved success")
}

对于未来的读者来说,这就是我使用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)

您可以查看@ovo,谢谢您的支持,但它与swift有关吗?请删除
NSKeyedArchiver
部分。只需直接调用
response.data.write(…)
。要在web视图中打开PDF,您只需从该文件中获取
URL
,然后加载它。@Sulthan感谢您的支持,我知道在web视图中打开该文件,没有问题,但这是保存部分。感谢您的支持,对此答案有任何进一步的解释吗?谢谢,我会尝试一下并让您知道:)您可以选中[ifunbbox]()以显示文档目录下生成的文件。@FaresBenHamouda如何在应用程序中查看pdf?。那会有帮助的
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)