NCViewerPhotoTilingView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import UIKit
  2. class NCViewerPhotoTilingView: UIView {
  3. var imageName: String
  4. var url: URL
  5. var tilingView: NCViewerPhotoTilingView?
  6. // We use these two properties to avoid accessing tiledLayer from bg thread:
  7. var storedTileSize: CGSize!
  8. var storedBounds: CGRect!
  9. override class var layerClass: AnyClass {
  10. return CATiledLayer.self
  11. }
  12. // returns layer property as CATiledLayer
  13. var tiledLayer: CATiledLayer {
  14. return self.layer as! CATiledLayer
  15. }
  16. /*
  17. Force contentScaleFactor of 1, even on retina displays For the CATiledLayer
  18. to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the
  19. tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens,
  20. which would cause the CATiledLayer to ask us for tiles of the wrong scales.)
  21. */
  22. override var contentScaleFactor: CGFloat {
  23. didSet {
  24. super.contentScaleFactor = 1
  25. }
  26. }
  27. init(in url: URL, size: CGSize) {
  28. self.url = url
  29. self.imageName = url.deletingPathExtension().lastPathComponent
  30. super.init(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
  31. tiledLayer.levelsOfDetail = 4
  32. storedTileSize = tiledLayer.tileSize
  33. storedBounds = self.bounds
  34. }
  35. required init?(coder aDecoder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. // Only override draw() if you perform custom drawing.
  39. // An empty implementation adversely affects performance during animation.
  40. override func draw(_ rect: CGRect) {
  41. let context = UIGraphicsGetCurrentContext()!
  42. // get the scale from the context by getting the current transform matrix, then asking for
  43. // its "a" component, which is one of the two scale components. We need to also ask for the "d" component as it might not be precisely the same as the "a" component, even at the "same" scale.
  44. let scaleX: CGFloat = context.ctm.a
  45. let scaleY: CGFloat = context.ctm.d
  46. var tileSize = self.storedTileSize!
  47. // Even at scales lower than 100%, we are drawing into a rect in the coordinate system of the full
  48. // image. One tile at 50% covers the width (in original image coordinates) of two tiles at 100%.
  49. // So at 50% we need to stretch our tiles to double the width and height; at 25% we need to stretch
  50. // them to quadruple the width and height; and so on.
  51. // (Note that this means that we are drawing very blurry images as the scale gets low. At 12.5%,
  52. // our lowest scale, we are stretching about 6 small tiles to fill the entire original image area.
  53. // But this is okay, because the big blurry image we're drawing here will be scaled way down before
  54. // it is displayed.)
  55. tileSize.width /= scaleX
  56. tileSize.height /= -scaleY
  57. // calculate the rows and columns of tiles that intersect the rect we have been asked to draw
  58. let firstCol: Int = Int(floor(rect.minX/tileSize.width))
  59. let lastCol: Int = Int(floor((rect.maxX-1)/tileSize.width))
  60. let firstRow: Int = Int(floor(rect.minY/tileSize.height))
  61. let lastRow: Int = Int(floor((rect.maxY-1)/tileSize.height))
  62. for row in firstRow...lastRow {
  63. for col in firstCol...lastCol {
  64. guard let tile = tileFor(scale: scaleX, row: row, col: col) else {
  65. return
  66. }
  67. var tileRect = CGRect(x: tileSize.width*CGFloat(col), y: tileSize.height*CGFloat(row), width: tileSize.width, height: tileSize.height)
  68. // if the tile would stick outside of our bounds, we need to truncate it so as
  69. // to avoid stretching out the partial tiles at the right and bottom edges
  70. tileRect = self.storedBounds.intersection(tileRect)
  71. tile.draw(in: tileRect)
  72. }
  73. }
  74. }
  75. func tileFor(scale: CGFloat, row: Int, col: Int) -> UIImage? {
  76. //this accounts for a bug somewhere upstream that returns the scale as a floating point number just below the required value: 0.249... instead of 0.2500
  77. let scale = scale < 1.0 ? Int(1/CGFloat(Int(1/scale))*1000) : Int(scale*1000)
  78. // we use "UIImage(contentsOfFile:)" instead of "UIImage(named:)" here because we don't
  79. // want UIImage to cache our tiles
  80. let tileName = "\(self.imageName)_\(scale)_\(col)_\(row).png"
  81. let path = url.appendingPathComponent(tileName).path
  82. let image = UIImage(contentsOfFile: path)
  83. return image
  84. }
  85. }