123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import UIKit
- class NCViewerPhotoTilingView: UIView {
-
- var imageName: String
- var url: URL
- var tilingView: NCViewerPhotoTilingView?
-
- var storedTileSize: CGSize!
- var storedBounds: CGRect!
-
- override class var layerClass: AnyClass {
- return CATiledLayer.self
- }
-
- var tiledLayer: CATiledLayer {
- return self.layer as! CATiledLayer
- }
-
- override var contentScaleFactor: CGFloat {
- didSet {
- super.contentScaleFactor = 1
- }
- }
-
- init(in url: URL, size: CGSize) {
- self.url = url
- self.imageName = url.deletingPathExtension().lastPathComponent
-
- super.init(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
- tiledLayer.levelsOfDetail = 4
-
- storedTileSize = tiledLayer.tileSize
- storedBounds = self.bounds
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
-
-
- override func draw(_ rect: CGRect) {
-
- let context = UIGraphicsGetCurrentContext()!
-
-
- let scaleX: CGFloat = context.ctm.a
- let scaleY: CGFloat = context.ctm.d
- var tileSize = self.storedTileSize!
-
-
-
-
-
-
-
-
-
-
- tileSize.width /= scaleX
- tileSize.height /= -scaleY
-
- let firstCol: Int = Int(floor(rect.minX/tileSize.width))
- let lastCol: Int = Int(floor((rect.maxX-1)/tileSize.width))
- let firstRow: Int = Int(floor(rect.minY/tileSize.height))
- let lastRow: Int = Int(floor((rect.maxY-1)/tileSize.height))
- for row in firstRow...lastRow {
- for col in firstCol...lastCol {
- guard let tile = tileFor(scale: scaleX, row: row, col: col) else {
- return
- }
- var tileRect = CGRect(x: tileSize.width*CGFloat(col), y: tileSize.height*CGFloat(row), width: tileSize.width, height: tileSize.height)
-
-
-
- tileRect = self.storedBounds.intersection(tileRect)
- tile.draw(in: tileRect)
- }
- }
- }
-
- func tileFor(scale: CGFloat, row: Int, col: Int) -> UIImage? {
-
- let scale = scale < 1.0 ? Int(1/CGFloat(Int(1/scale))*1000) : Int(scale*1000)
-
-
-
- let tileName = "\(self.imageName)_\(scale)_\(col)_\(row).png"
-
- let path = url.appendingPathComponent(tileName).path
- let image = UIImage(contentsOfFile: path)
- return image
- }
- }
|