嗨,我正在尝试将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)