ImagePreviewItem.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. class ImagePreviewItem: MediaPreviewItem, MediaPreviewItemProtocol {
  23. private var image: Data?
  24. override func requestAsset() {
  25. let manager = PHImageManager()
  26. guard let originalAsset = self.originalAsset else {
  27. guard let itemUrl = self.itemUrl else {
  28. return
  29. }
  30. do {
  31. self.image = try Data(contentsOf: itemUrl)
  32. } catch {
  33. DDLogError(error.localizedDescription)
  34. }
  35. if self.image != nil {
  36. self.uti = UTIConverter.uti(forFileURL: itemUrl)
  37. self.semaphore.signal()
  38. self.initThumbnail()
  39. }
  40. return
  41. }
  42. let asset = originalAsset.originalAsset!
  43. if asset.mediaType == PHAssetMediaType.image {
  44. let options = PHImageRequestOptions()
  45. options.isSynchronous = true
  46. options.isNetworkAccessAllowed = true
  47. manager.requestImageData(for: originalAsset.originalAsset!, options: options, resultHandler: { (data, dataUTI, _, _) in
  48. let resources = PHAssetResource.assetResources(for: asset)
  49. var orgFilename = "File"
  50. if resources.count > 0 {
  51. orgFilename = resources.first!.originalFilename
  52. }
  53. self.filename = orgFilename
  54. self.uti = dataUTI
  55. guard let data = data else {
  56. return
  57. }
  58. self.image = data
  59. self.semaphore.signal()
  60. self.initThumbnail()
  61. })
  62. }
  63. }
  64. private func initThumbnail() {
  65. guard let data = self.image else {
  66. return
  67. }
  68. let dataImage = UIImage(data: data)
  69. self.thumbnail = MediaConverter.getThumbnailFor(dataImage)
  70. self.thumbnailSemaphore.signal()
  71. }
  72. override func getAccessiblityDescription() -> String? {
  73. guard let originalAsset = self.originalAsset else {
  74. let text = String(format: NSLocalizedString("image", comment: ""))
  75. return text
  76. }
  77. let asset = originalAsset.originalAsset!
  78. guard let date = asset.creationDate else {
  79. return nil
  80. }
  81. let datetime = DateFormatter.accessibilityDateTime(date)
  82. let text = String(format: BundleUtil.localizedString(forKey: "imagedate_date"), datetime)
  83. return text
  84. }
  85. func getItem() -> Data? {
  86. if self.image == nil {
  87. self.semaphore.wait()
  88. self.semaphore.signal()
  89. }
  90. return self.image
  91. }
  92. }