27
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftで画像をPDFに変換させる

Last updated at Posted at 2015-12-14

この記事は学生エンジニア Advent Calendar15日目の記事です。

どこで使うんだよと言われればそれまでですが、以前インターン先で作ったので、共有しときます。

###使用した環境
Xcode 7.2
Swift 2.1
iOS 9.2(実機で確認 *なぜかSimulatorは動かない)

###必要なフレームワーク

  • SystemConfiguration.framework
  • StoreKit.framework
  • Security.framework
  • QuartzCore.framework
  • MobileCoreServices.framework
  • CoreLocation.framework
  • CoreGraphics.framework

##全体のコード

ViewController.swift
import UIKit

class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {

    var pdfData: NSData?
    var docController:UIDocumentInteractionController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var sampleImages: Array<UIImage> = []
        
        sampleImages.append(UIImage(named: "g_sample001.jpg")!)
        sampleImages.append(UIImage(named: "g_sample001.jpg")!)
        
        pdfData = self.createPDF(sampleImages)!
        
    }

    @IBAction func showOpenIn(sender: AnyObject) {
        let fileName = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("tmp_file.pdf")
        let url: NSURL! = NSURL(fileURLWithPath: fileName)
        pdfData!.writeToURL(url, atomically: true)
        
        if url != nil {
            docController = UIDocumentInteractionController(URL: url)
            docController?.delegate = self
            docController?.presentOptionsMenuFromRect(self.view.frame, inView: self.view, animated: true)
        }
       
        if NSFileManager.defaultManager().fileExistsAtPath(fileName) {
            print("existed")
        }
    }
    
    
    //convert all images to a pdf
    func createPDF(images: [UIImage]) -> NSData? {
        //16:9 default image size
        let imageWidth: CGFloat = 1136
        let imageHeight: CGFloat = 640
        
        let pdfData: NSMutableData = NSMutableData()
        let pdfConsumer: CGDataConsumerRef = CGDataConsumerCreateWithCFData(pdfData as CFMutableDataRef)!
        
        var mediaBox: CGRect = CGRectMake(0, 0, imageWidth, imageHeight)
        
        let pdfContext: CGContextRef = CGPDFContextCreate(pdfConsumer, &mediaBox, nil)!
        
        for var index = 0; index < images.count; ++index {
            CGContextBeginPage(pdfContext, &mediaBox)
            CGContextDrawImage(pdfContext, mediaBox, images[index].CGImage)
            CGContextEndPage(pdfContext)
        }
        
        return pdfData
    }
    
}

流れとしては
PDFに変換したい画像を配列に入れる

配列に入っている画像をPDFに変換していく

共有ボタン(Share)が押されたら、showOpenInの関数が呼ばれて他のアプリへ共有出来るようにする
2015-12-15 08.03.05.png 2015-12-15 08.03.13.png

ちなみにGithubにもコード載せています。
https://github.com/satoichiki/ConvertImageToPdf

27
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?