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:
Nihaal Sharma committed 2026-03-06 23:46:09 +00:00
1 parent 23670efcfb
commit 2b8c636945
10 files changed
+215 -61

No files matched your search

+71
View File
@@ -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 }
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.