FilePreviewController.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // FilePreviewController.swift
  3. // Runner
  4. //
  5. // Created by FancyLou on 2023/5/23.
  6. //
  7. import UIKit
  8. import QuickLook
  9. class FilePreviewController: QLPreviewController {
  10. var currentFileURLS:[NSURL] = []
  11. override var preferredStatusBarStyle: UIStatusBarStyle {
  12. return .lightContent
  13. }
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. self.delegate = self
  17. self.dataSource = self
  18. if let url = self.currentFileURLS[0].path {
  19. if url.lowercased().hasSuffix(".png") || url.lowercased().hasSuffix(".jpg") || url.lowercased().hasSuffix(".jpeg") {
  20. self.loadImageDownloadBtn()
  21. }
  22. }
  23. }
  24. private func loadImageDownloadBtn() {
  25. let downImageBtn = UIImageView(frame: CGRect(x: UIScreen.main.bounds.width - 48 - 12 , y: UIScreen.main.bounds.height - 48 - 10, width: 48, height: 48))
  26. downImageBtn.image = UIImage(named: "icon_download")
  27. self.view.addSubview(downImageBtn)
  28. downImageBtn.isHidden = false
  29. downImageBtn.addTapGesture { tap in
  30. self.saveImageToAlbum()
  31. }
  32. }
  33. private func saveImageToAlbum() {
  34. print("保存图片到相册!")
  35. if let url = self.currentFileURLS[0].path {
  36. print("path: \(url)")
  37. UIImageWriteToSavedPhotosAlbum(UIImage(contentsOfFile: url)!, self, #selector(self.saveImage(image:didFinishSavingWithError:contextInfo:)), nil)
  38. }
  39. }
  40. @objc func saveImage(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafeRawPointer) {
  41. if let e = error {
  42. print(e.localizedDescription)
  43. //self.showError(title: "保存图片失败!")
  44. } else {
  45. //self.showSuccess(title: "保存图片到相册成功!")
  46. print("保存图片到相册成功!")
  47. }
  48. }
  49. }
  50. extension FilePreviewController: QLPreviewControllerDelegate,QLPreviewControllerDataSource {
  51. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  52. return self.currentFileURLS.count
  53. }
  54. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  55. return self.currentFileURLS[index]
  56. }
  57. }