我们可以用铅笔套件在照片标记中添加文本、形状和签名吗



在我的应用程序中使用PencelKit标记照片时,我没有任何添加文本、形状和签名的选项。此选项在苹果的照片应用程序中可用。我曾尝试使用CanvasView和PKToolPicker的各种属性访问它,但没有成功。

self.canvasView?.drawing = PKDrawing()
self.canvasView.allowsFingerDrawing = true
if let window = self.view.window, let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.setVisible(true, forFirstResponder: self.canvasView)
toolPicker.addObserver(self.canvasView)
self.canvasView.becomeFirstResponder()
}

我终于想通了!它是QLPreviewController

使用形状、箭头和签名进行编辑仅适用于iOS13+。首先,我们需要从url中读取文件,因此使用init进行设置。我用这样的东西作为基础,并附加文件名,还有文件扩展名,例如.pdf:

FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!

无法将其保存在tempDirectory中,因为QLPreviewViewController需要访问权限。没有权限可能导致这样的错误:

AX Lookup problem - errorCode:1100 error:Permission denied portName:

你的customVC应该是这样的:

import UIKit
import QuickLook
class CustomVC: UIViewController {
var url: URL    
init(url: URL) {
self.url = url
}
....func viewDidLoad() and stuff ......
func editFile() {
let editor = QLPreviewController()
editor.dataSource = self
editor.delegate = self
editor.setEditing(true, animated: true)
present(editor, animated: true, completion: nil)
}
}
// Load the file in the QLPreviewController with DataSource
extension CustomVC: QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.url! as QLPreviewItem
}  
}
// Make editing available with Delegate
@available(iOS 13.0, *)
extension CustomVC: QLPreviewControllerDelegate {

func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
return .updateContents
}

func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
print("UPDATE")
}

func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
print("SAVED at (modifiedContentsURL)")
}
}

如果您在委托中正确实现了这些功能,则标记按钮将自动显示。

您也可以像正常情况一样,通过一个额外的navigationController为这个VC添加更多的barButtonItems,例如在editFile函数中这样的东西:

let navController = UINavigationController(rootViewController: editor)
let customButton = UIBarButtonItem(image: UIImage(systemName: "yourImageName"), style: .plain, target: self, action: #selector(customButtonTapped(_:)))
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped(_:)))
if var items = editor.navigationItem.rightBarButtonItems {
items.append(customButton)
editor.navigationItem.rightBarButtonItems = items
} else {
editor.navigationItem.rightBarButtonItems = [customButton]
}
editor.navigationItem.leftBarButtonItem = doneButton
viewController?.present(navController, animated: true, completion: nil)
self.navigationController = navController

相关内容

  • 没有找到相关文章

最新更新