71 lines
1.8 KiB
Swift
71 lines
1.8 KiB
Swift
//
|
|
// ImageView.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Juraldinio on 9/1/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
typealias ImageViewAction = () -> Void
|
|
|
|
final class ImageView: UIImageView {
|
|
|
|
var roundImage = false {
|
|
didSet {
|
|
self.layer.cornerRadius = self.roundImage ? self.bounds.width / 2.0 : 0
|
|
self.layer.masksToBounds = true
|
|
}
|
|
}
|
|
|
|
var action: ImageViewAction?
|
|
|
|
var imageUrl: URL? {
|
|
didSet {
|
|
guard let url = self.imageUrl else {
|
|
self.image = nil
|
|
return
|
|
}
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
|
|
guard let image = UIImage(contentsOfFile: url.absoluteString) else { return }
|
|
DispatchQueue.main.async {
|
|
self?.image = image
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
override init(image: UIImage?) {
|
|
super.init(image: image)
|
|
self.setup()
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
self.setup()
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
private func setup() {
|
|
self.contentMode = .scaleAspectFill
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(tapGestureRecognizer:)))
|
|
self.addGestureRecognizer(tapGesture)
|
|
}
|
|
|
|
@objc
|
|
private func tapAction(tapGestureRecognizer: UITapGestureRecognizer) {
|
|
guard let action = self.action else { return }
|
|
action()
|
|
}
|
|
}
|