A WIP iOS app for Mastodon and Pleroma.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

//
// AttachmentView.swift
// Tusker
//
// Created by Shadowfacts on 8/31/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
import Gifu
protocol AttachmentViewDelegate {
func showLargeAttachment(for attachmentView: AttachmentView)
}
class AttachmentView: UIImageView, GIFAnimatable {
var delegate: AttachmentViewDelegate?
var attachment: Attachment!
var gifData: Data?
public lazy var animator: Animator? = Animator(withDelegate: self)
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) { (data) in
guard let data = data else { return }
DispatchQueue.main.async {
if self.attachment.url.pathExtension == "gif" {
self.animate(withGIFData: data)
self.gifData = data
} else {
self.image = UIImage(data: data)
}
}
}
}
override func display(_ layer: CALayer) {
updateImageIfNeeded()
}
@objc func imagePressed() {
if image != nil {
delegate?.showLargeAttachment(for: self)
}
}
}