我正在尝试使用swift创建一个iOS应用程序,它可以让用户拍摄照片或从图库中选择图像,并将其转换为pdf文件,以便保存到手机中。我的代码目前可以打开相机或图库并选择图像,但我无法将其转换为pdf。如有任何提示,我将不胜感激,谢谢!
相机视图控制器类
import UIKit
class CameraViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
@IBOutlet weak var myImg: UIImageView!
@IBAction func takePhoto(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImg.contentMode = .scaleToFill
myImg.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
@IBAction func savePhoto(_ sender: AnyObject) {
let imageData = UIImagePNGRepresentation(myImg.image!)
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
let alert = UIAlertController(title: "Saved", message: "Your image has been saved", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
画廊视图控制器类
import UIKit
class GalleryViewController: UIViewController {
@IBOutlet weak var myImg: UIImageView!
@IBAction func pickPhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImg.contentMode = .scaleToFill
myImg.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
5条答案
按热度按时间xam8gpfp1#
答案更新日期:
由于苹果在iOS 11.0中引入了PDFKit,你可以使用下面的代码将uiimage转换为pdf,我只尝试了下面的OSX,但在iOS上应该也是这样。
================================================
其实有很多类似的问题,答案也很好,让我再回答一遍。
基本上生成PDF类似于iOS中的绘图。
1.创建PDF上下文并将其推送到图形堆栈上。
1.创建一个页面。
1.使用UIKit或Core Graphics例程绘制页面的内容。
1.根据需要添加链接。
1.根据需要重复步骤2、3和4。
1.结束PDF上下文以从图形堆栈中弹出上下文,并根据上下文的创建方式,将生成的数据写入指定的PDF文件或存储到指定的NSMutableData对象中。
所以最简单的方法是这样的:
这为PDF文件创建了所有NSData,然后我们需要将数据保存到文件:
在此阅读更多信息:生成PDF内容
vfh0ocws2#
在swift 5中使用PDFKit:首次导入PDFKit
然后使用此阵列扩展:
并使用以下内容:
let imageArray = [UIImage(named: "1")!,UIImage(named: "2")!] let yourPDF = imageArray.makePDF()
hof1towb3#
Swift 5我们将使用UIGraphicsPDFRenderer()类,它将适用于iOS 10+
3yhwsihp4#
在www.example.com中编写的PDF生成器swift.it将帮助生成包含图像路径、图像二进制、图像参考(CGImage)的PDF
https://github.com/sgr-ksmt/PDFGenerator
x6yk4ghg5#