mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 12:44:51 +00:00
adding gifs
added sticker.swift to add a protocol for stickers and an extension that has shared default implementations of functions and computed properties added stickertype enum
This commit is contained in:
1 parent
23670efcfb
commit
2b8c636945
10 files changed
+215
-61
No files matched your search
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Sticker.swift
|
||||
// StickerSlack
|
||||
//
|
||||
// Created by neon443 on 06/03/2026.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Messages
|
||||
|
||||
extension StickerProtocol {
|
||||
var localImageURL: URL {
|
||||
return URL(string: localImageURLString)!
|
||||
}
|
||||
|
||||
var isLocal: Bool {
|
||||
return (try? Data(contentsOf: localImageURL)) != nil
|
||||
}
|
||||
|
||||
var msSticker: MSSticker? {
|
||||
guard isLocal else {
|
||||
return nil
|
||||
}
|
||||
return try? MSSticker(contentsOfFileURL: localImageURL, localizedDescription: name)
|
||||
}
|
||||
|
||||
var image: UIImage? {
|
||||
if let data = try? Data(contentsOf: localImageURL),
|
||||
let img = UIImage(data: data) {
|
||||
return img
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func deleteImage() {
|
||||
try? FileManager.default.removeItem(at: localImageURL)
|
||||
return
|
||||
}
|
||||
|
||||
func resize(image: UIImage, to targetSize: CGSize) -> UIImage {
|
||||
let oldSize = image.size
|
||||
let ratio: (x: CGFloat, y: CGFloat)
|
||||
ratio.x = targetSize.width / oldSize.width
|
||||
ratio.y = targetSize.height / oldSize.height
|
||||
|
||||
var newSize: CGSize
|
||||
if ratio.x > ratio.y {
|
||||
newSize = CGSize(width: oldSize.width * ratio.y, height: oldSize.height * ratio.y)
|
||||
} else {
|
||||
newSize = CGSize(width: oldSize.width * ratio.x, height: oldSize.height * ratio.x)
|
||||
}
|
||||
|
||||
let rect = CGRect(origin: .zero, size: newSize)
|
||||
|
||||
if let frames = image.images {
|
||||
var result: [UIImage] = []
|
||||
for frame in frames {
|
||||
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
|
||||
frame.draw(in: rect)
|
||||
result.append(UIGraphicsGetImageFromCurrentImageContext() ?? UIImage())
|
||||
UIGraphicsEndImageContext()
|
||||
}
|
||||
return UIImage.animatedImage(with: result, duration: image.duration) ?? UIImage()
|
||||
}
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
|
||||
image.draw(in: rect)
|
||||
return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// StickerProtocol.swift
|
||||
// StickerSlack
|
||||
//
|
||||
// Created by neon443 on 06/03/2026.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Messages
|
||||
|
||||
protocol StickerProtocol: Codable, Identifiable, Hashable {
|
||||
var id: UUID { get set }
|
||||
var uiID: UUID { get set }
|
||||
var name: String { get set }
|
||||
|
||||
var localImageURL: URL { get }
|
||||
var localImageURLString: String { get }
|
||||
var remoteImageURL: URL { get set }
|
||||
|
||||
var isLocal: Bool { get }
|
||||
|
||||
var msSticker: MSSticker? { get }
|
||||
var image: UIImage? { get }
|
||||
|
||||
func downloadImage() async throws
|
||||
func deleteImage()
|
||||
mutating func refresh()
|
||||
func resize(image: UIImage, to targetSize: CGSize) -> UIImage
|
||||
static var test: Self { get }
|
||||
}
|
||||