60 lines
1.3 KiB
Swift
60 lines
1.3 KiB
Swift
//
|
|
// AttachmentView.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/31/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
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() {
|
|
ImageCache.attachments.get(attachment.url) { (image) in
|
|
DispatchQueue.main.async {
|
|
self.image = image
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func imagePressed() {
|
|
if image != nil {
|
|
delegate?.showLargeAttachment(for: self)
|
|
}
|
|
}
|
|
|
|
}
|