小编Vic*_*Vic的帖子

将pdf的NSData转换为图像文件的NSData

嗨,我正在尝试将pdf文件另存为jpg文件。我有许多要查询的网址,这些网址链接都是1页pdf文件。

var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let urlRequest = NSURLRequest(URL: NSURL(string: downloadUrl[index])!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in

  var savePath = path.stringByAppendingPathComponent(file_name + ".jpg")

  if let httpResponse = response as? NSHTTPURLResponse {
    if let contentType = httpResponse.allHeaderFields["Content-Type"] as? NSString {
      let fileType = contentType.componentsSeparatedByString("/").last! as! String
      if fileType == "pdf" {

        // convert to jpg before saving!

        NSFileManager.defaultManager().createFileAtPath(savePath, contents: convertedData, attributes: nil)
      } else {
        NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
      }

      self.tableView.reloadData()
    } …
Run Code Online (Sandbox Code Playgroud)

pdf nsdata ios swift

5
推荐指数
0
解决办法
2114
查看次数

标签 统计

ios ×1

nsdata ×1

pdf ×1

swift ×1