Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Last active August 29, 2015 14:15
Show Gist options
  • Save kazukitanaka0611/00899e1e35e95afdb19c to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/00899e1e35e95afdb19c to your computer and use it in GitHub Desktop.
PDFView
import UIKit
class PDFViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
private var imageList: [UIImage] = []
var fileName: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if self.fileName == nil {
self.showErrorAlert()
return
}
self.readPDFDocument()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func changePage(sender: UIPageControl) {
var frame = self.scrollView.frame
frame.origin.x = CGFloat(frame.size.width) * CGFloat(self.pageControl.currentPage)
frame.origin.y = 0
self.scrollView.scrollRectToVisible(frame, animated: true)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let pageWidth = self.scrollView.frame.size.width
self.pageControl.currentPage = Int(floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
}
private func readPDFDocument() {
let fileManger = NSFileManager.defaultManager()
let bundle = NSBundle.mainBundle()
var url: NSURL! = nil
if (fileManger.fileExistsAtPath(bundle.bundlePath + "/" + self.fileName)) {
url = NSBundle.mainBundle().URLForResource(self.fileName, withExtension:nil)!
} else {
let doccumentDirectries = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = doccumentDirectries.last as String
if (fileManger.fileExistsAtPath(documentDirectory + "/" + self.fileName)) {
url = NSURL(fileURLWithPath: documentDirectory + "/" + self.fileName)
} else {
self.showErrorAlert()
return
}
}
let pdf = CGPDFDocumentCreateWithURL(url);
let pageCount = CGPDFDocumentGetNumberOfPages(pdf)
NSLog("pageCount %d",pageCount)
self.pageControl.numberOfPages = Int(pageCount)
var contentWidth: CGFloat = 0
for (var i = 1; i <= Int(pageCount); i++) {
let page = CGPDFDocumentGetPage(pdf, UInt(i))
var pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
let pdfScale = self.view.frame.size.width / pageRect.size.width
pageRect.size = CGSize(width: pageRect.size.width * pdfScale,
height: pageRect.size.height * pdfScale)
pageRect.origin = CGPoint(x: pageRect.size.width * CGFloat(i-1), y: 0)
contentWidth += CGFloat(pageRect.size.width)
UIGraphicsBeginImageContext(pageRect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0)
CGContextFillRect(context, pageRect)
CGContextSaveGState(context)
CGContextTranslateCTM(context, 0.0, pageRect.size.height)
CGContextScaleCTM(context, pdfScale, -pdfScale)
CGContextDrawPDFPage(context, page)
CGContextRestoreGState(context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.imageList.append(image)
let imageView = UIImageView(image: image)
imageView.frame = pageRect
imageView.contentMode = UIViewContentMode.ScaleAspectFit
self.scrollView.addSubview(imageView)
}
self.scrollView.contentSize = CGSize(width: contentWidth, height: self.scrollView.frame.height)
}
private func showErrorAlert() {
let alertController = UIAlertController(title: "Error", message: "Fail Document!", preferredStyle: .Alert)
alertController.addAction(
UIAlertAction(title: "OK",
style: UIAlertActionStyle.Default,
handler:{ (action:UIAlertAction!) -> Void in
println("Cancel")
self.navigationController?.popViewControllerAnimated(true)
}))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment