Tusker/Tusker/Views/AttachmentView.swift

60 lines
1.3 KiB
Swift
Raw Normal View History

2018-09-02 20:59:20 +00:00
//
// AttachmentView.swift
// Tusker
//
// Created by Shadowfacts on 8/31/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-09-02 20:59:20 +00:00
protocol AttachmentViewDelegate {
func showLargeAttachment(for attachmentView: AttachmentView)
}
class AttachmentView: UIImageView {
var delegate: AttachmentViewDelegate?
var attachment: Attachment!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
convenience init(frame: CGRect, attachment: Attachment) {
self.init(frame: frame)
self.attachment = attachment
loadImage()
}
func commonInit() {
contentMode = .scaleAspectFill
layer.masksToBounds = true
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed)))
}
func loadImage() {
2018-10-03 12:53:05 +00:00
ImageCache.attachments.get(attachment.url) { (image) in
2018-09-02 20:59:20 +00:00
DispatchQueue.main.async {
self.image = image
}
}
}
@objc func imagePressed() {
if image != nil {
delegate?.showLargeAttachment(for: self)
}
}
}