Tusker/Tusker/Views/Attachments/AttachmentView.swift

112 lines
3.2 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-11-09 20:48:08 +00:00
import Gifu
2019-09-10 16:25:50 +00:00
import AVFoundation
2018-09-02 20:59:20 +00:00
protocol AttachmentViewDelegate: class {
func showAttachmentsGallery(startingAt index: Int)
2018-09-02 20:59:20 +00:00
}
2018-11-09 20:48:08 +00:00
class AttachmentView: UIImageView, GIFAnimatable {
2018-09-02 20:59:20 +00:00
weak var delegate: AttachmentViewDelegate?
2019-09-10 16:25:50 +00:00
var playImageView: UIImageView!
2018-09-02 20:59:20 +00:00
var attachment: Attachment!
var index: Int!
2018-11-09 20:48:08 +00:00
var gifData: Data?
2018-11-09 20:48:08 +00:00
public lazy var animator: Animator? = Animator(withDelegate: self)
init(attachment: Attachment, index: Int) {
super.init(image: nil)
2018-09-02 20:59:20 +00:00
commonInit()
self.attachment = attachment
self.index = index
2019-09-10 16:25:50 +00:00
loadAttachment()
2018-09-02 20:59:20 +00:00
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
2018-09-02 20:59:20 +00:00
commonInit()
}
func commonInit() {
contentMode = .scaleAspectFill
layer.masksToBounds = true
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed)))
2019-09-10 16:25:50 +00:00
playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
playImageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(playImageView)
NSLayoutConstraint.activate([
playImageView.widthAnchor.constraint(equalToConstant: 50),
playImageView.heightAnchor.constraint(equalToConstant: 50),
playImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
playImageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
func loadAttachment() {
switch attachment.kind {
case .image:
loadImage()
case .video:
loadVideo()
default:
fatalError()
}
2018-09-02 20:59:20 +00:00
}
func loadImage() {
ImageCache.attachments.get(attachment.url) { [weak self] (data) in
guard let self = self, let data = data else { return }
2018-09-02 20:59:20 +00:00
DispatchQueue.main.async {
2018-11-09 20:48:08 +00:00
if self.attachment.url.pathExtension == "gif" {
self.animate(withGIFData: data)
self.gifData = data
} else {
self.image = UIImage(data: data)
}
2018-09-02 20:59:20 +00:00
}
}
2019-09-10 16:25:50 +00:00
playImageView.isHidden = true
}
func loadVideo() {
DispatchQueue.global(qos: .userInitiated).async {
let asset = AVURLAsset(url: self.attachment.url)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
guard let image = try? generator.copyCGImage(at: CMTime(seconds: 0, preferredTimescale: 1), actualTime: nil) else { return }
DispatchQueue.main.async {
self.image = UIImage(cgImage: image)
}
}
playImageView.isHidden = false
2018-09-02 20:59:20 +00:00
}
2018-11-09 20:48:08 +00:00
override func display(_ layer: CALayer) {
updateImageIfNeeded()
}
2018-09-02 20:59:20 +00:00
@objc func imagePressed() {
delegate?.showAttachmentsGallery(startingAt: index)
2018-09-02 20:59:20 +00:00
}
}