rewrote resize funciton

This commit is contained in:
neon443 committed 2026-05-03 16:12:26 +01:00
1 parent fd8e7f1fe5
commit 9f60f1a316
6 files changed
+77 -66

No files matched your search

-5
View File
@@ -201,11 +201,6 @@ class EmojiHoarder: BaseHoarder {
for emoji in emojisToDownload { for emoji in emojisToDownload {
group.addTask { group.addTask {
await self.download(emoji: emoji, skipStoreIndex: true) await self.download(emoji: emoji, skipStoreIndex: true)
await MainActor.run {
// Task { @MainActor in
self.downloadedStickers.insert(emoji.name)
self.downloadedStickersArr.append(emoji.name)
}
} }
} }
} }
+33 -5
View File
@@ -7,6 +7,7 @@
import Foundation import Foundation
import UIKit import UIKit
import UniformTypeIdentifiers
class GifManager { class GifManager {
//from clock-run, 12 frames one second //from clock-run, 12 frames one second
@@ -39,12 +40,39 @@ class GifManager {
} }
static func dataFrom(frames: [(frame: CGImage, showFor: Double)]) -> Data? { static func dataFrom(frames: [(frame: CGImage, showFor: Double)]) -> Data? {
// let cgImage = CGImageSourceCreateWithData(<#T##data: CFData##CFData#>, <#T##options: CFDictionary?##CFDictionary?#>) var data = NSMutableData()
return nil
let fileProperties = [
kCGImagePropertyGIFDictionary: [
kCGImagePropertyGIFLoopCount: 0
]
]
guard let destination = CGImageDestinationCreateWithData(
data,
UTType.gif.identifier as CFString,
frames.count,
nil
) else { fatalError() }
CGImageDestinationSetProperties(destination, fileProperties as NSDictionary)
for frame in frames {
autoreleasepool {
let frameProperties = [
kCGImagePropertyGIFDictionary: [
kCGImagePropertyGIFDelayTime: frame.showFor
]
]
CGImageDestinationAddImage(
destination,
frame.frame,
frameProperties as CFDictionary
)
}
} }
static func dataFrom(gif: CGImage) -> Data? { guard CGImageDestinationFinalize(destination) else { fatalError() }
return data as Data
return nil
} }
} }
+1 -1
View File
@@ -47,7 +47,7 @@ struct Gif: StickerProtocol {
let cgImage = uiImage.cgImage, let cgImage = uiImage.cgImage,
!self.localImageURLString.contains(".gif"), !self.localImageURLString.contains(".gif"),
cgImage.width < 300 || cgImage.height < 300 { cgImage.width < 300 || cgImage.height < 300 {
data = resize(image: uiImage, to: CGSize(width: 300, height: 300)).pngData()! data = await resize(image: uiImage, to: CGSize(width: 300, height: 300)).pngData()!
} }
try! data.write(to: localImageURL) try! data.write(to: localImageURL)
+24 -40
View File
@@ -61,7 +61,6 @@ extension StickerProtocol {
nonisolated nonisolated
func downloadImage() async throws { func downloadImage() async throws {
let targetSize = CGSize(width: 500, height: 500)
if let data = try? await Data(contentsOf: localImageURL), if let data = try? await Data(contentsOf: localImageURL),
let _ = UIImage(data: data) { let _ = UIImage(data: data) {
return return
@@ -69,23 +68,13 @@ extension StickerProtocol {
var (data, _) = try await URLSession.shared.data(from: remoteImageURL) var (data, _) = try await URLSession.shared.data(from: remoteImageURL)
print(await GifManager.gifFrom(data: data).count) let targetSize = CGSize(width: 600, height: 600)
if let uiImage = UIImage(data: data), if let uiImage = UIImage(data: data),
await GifManager.gifFrom(data: data).count == 1 { await localImageURLString.suffix(4) != ".gif" {
data = await resize(image: uiImage, to: targetSize).pngData()! data = await resize(image: uiImage, to: targetSize).pngData()!
} else {
var frames: [(frame: CGImage, showFor: Double)] = []
for frame in await GifManager.gifFrom(data: data) {
frames.append((
await resize(image: UIImage(cgImage: frame.frame), to: targetSize).cgImage!,
frame.showFor
))
}
data = await GifManager.dataFrom(frames: frames)!
} }
try! await data.write(to: localImageURL) try! await data.write(to: localImageURL)
return
} }
func deleteImage() { func deleteImage() {
@@ -93,34 +82,29 @@ extension StickerProtocol {
return return
} }
func resize(image: UIImage, to targetSize: CGSize) -> UIImage { nonisolated func resize(image: UIImage, to targetSize: CGSize) async -> UIImage {
let oldSize = image.size let renderer = UIGraphicsImageRenderer(size: targetSize)
let ratio: (x: CGFloat, y: CGFloat) let resizedImage = renderer.image { _ in
ratio.x = targetSize.width / oldSize.width image.draw(in: CGRect(origin: .zero, size: targetSize))
ratio.y = targetSize.height / oldSize.height }
return resizedImage
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) nonisolated func resize(cgImage: CGImage, to targetSize: CGSize) -> CGImage? {
let width = Int(targetSize.width)
let height = Int(targetSize.height)
guard let context = CGContext(
data: nil,
width: width,
height: height,
bitsPerComponent: cgImage.bitsPerComponent,
bytesPerRow: cgImage.bytesPerRow,
space: cgImage.colorSpace ?? CGColorSpaceCreateDeviceRGB(),
bitmapInfo: cgImage.bitmapInfo
) else { fatalError() }
if let frames = image.images { context.interpolationQuality = .default
var result: [UIImage] = [] context.draw(cgImage, in: CGRect(origin: .zero, size: targetSize))
for frame in frames { return context.makeImage()
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()
} }
} }
+1 -1
View File
@@ -24,6 +24,6 @@ protocol StickerProtocol: Codable, Identifiable, Hashable {
nonisolated func downloadImage() async throws nonisolated func downloadImage() async throws
nonisolated func deleteImage() async nonisolated func deleteImage() async
func resize(image: UIImage, to targetSize: CGSize) -> UIImage nonisolated func resize(image: UIImage, to targetSize: CGSize) async -> UIImage
static var test: Self { get } static var test: Self { get }
} }
@@ -15,6 +15,8 @@ struct EmojiPackDetailView: View {
@State var editName: Bool = false @State var editName: Bool = false
@State var editDescription: Bool = false @State var editDescription: Bool = false
@State var downloading: Bool = false
@State var showShare: Bool = false @State var showShare: Bool = false
@State var useSwiftUIGrid: Bool = false @State var useSwiftUIGrid: Bool = false
@@ -88,19 +90,6 @@ struct EmojiPackDetailView: View {
hoarder.saveEmojiPacks() hoarder.saveEmojiPacks()
} }
.toolbar { .toolbar {
ToolbarItem(placement: .topBarLeading) {
Circle()
.trim(from: 0, to: pack.downloadedFraction(in: hoarder))
.stroke(
.foreground,
style: StrokeStyle(
lineWidth: 5,
lineCap: .round
)
)
.rotationEffect(.degrees(-90))
.padding(5)
}
ToolbarItem(placement: .topBarLeading) { ToolbarItem(placement: .topBarLeading) {
Button( Button(
"", "",
@@ -112,7 +101,21 @@ struct EmojiPackDetailView: View {
.tint(edit ? Color.accentColor : .primary) .tint(edit ? Color.accentColor : .primary)
} }
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
Button("", systemImage: allDownloaded ? "checkmark" : "arrow.down") { if downloading {
ProgressView()
.onChange(of: allDownloaded) { newValue in
if newValue == true {
downloading = false
}
}
} else {
Button(
"",
systemImage: allDownloaded ? "checkmark" : "arrow.down"
) {
if !allDownloaded {
downloading = true
}
Task { Task {
if allDownloaded { if allDownloaded {
await pack.deleteAll(hoarder: hoarder) await pack.deleteAll(hoarder: hoarder)
@@ -123,6 +126,7 @@ struct EmojiPackDetailView: View {
} }
.tint(allDownloaded ? .red : .accentColor) .tint(allDownloaded ? .red : .accentColor)
} }
}
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
if #available(iOS 16, *) { if #available(iOS 16, *) {
ShareLink(item: pack.shareLink()) { ShareLink(item: pack.shareLink()) {
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.