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
+83 -72

No files matched your search

-5
View File
@@ -201,11 +201,6 @@ class EmojiHoarder: BaseHoarder {
for emoji in emojisToDownload {
group.addTask {
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 UIKit
import UniformTypeIdentifiers
class GifManager {
//from clock-run, 12 frames one second
@@ -39,12 +40,39 @@ class GifManager {
}
static func dataFrom(frames: [(frame: CGImage, showFor: Double)]) -> Data? {
// let cgImage = CGImageSourceCreateWithData(<#T##data: CFData##CFData#>, <#T##options: CFDictionary?##CFDictionary?#>)
return nil
}
var data = NSMutableData()
static func dataFrom(gif: CGImage) -> Data? {
let fileProperties = [
kCGImagePropertyGIFDictionary: [
kCGImagePropertyGIFLoopCount: 0
]
]
return nil
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
)
}
}
guard CGImageDestinationFinalize(destination) else { fatalError() }
return data as Data
}
}
+1 -1
View File
@@ -47,7 +47,7 @@ struct Gif: StickerProtocol {
let cgImage = uiImage.cgImage,
!self.localImageURLString.contains(".gif"),
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)
+24 -40
View File
@@ -61,7 +61,6 @@ extension StickerProtocol {
nonisolated
func downloadImage() async throws {
let targetSize = CGSize(width: 500, height: 500)
if let data = try? await Data(contentsOf: localImageURL),
let _ = UIImage(data: data) {
return
@@ -69,23 +68,13 @@ extension StickerProtocol {
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),
await GifManager.gifFrom(data: data).count == 1 {
await localImageURLString.suffix(4) != ".gif" {
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)
return
}
func deleteImage() {
@@ -93,34 +82,29 @@ extension StickerProtocol {
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)
nonisolated func resize(image: UIImage, to targetSize: CGSize) async -> UIImage {
let renderer = UIGraphicsImageRenderer(size: targetSize)
let resizedImage = renderer.image { _ in
image.draw(in: CGRect(origin: .zero, size: targetSize))
}
return resizedImage
}
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 {
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()
context.interpolationQuality = .default
context.draw(cgImage, in: CGRect(origin: .zero, size: targetSize))
return context.makeImage()
}
}
+1 -1
View File
@@ -24,6 +24,6 @@ protocol StickerProtocol: Codable, Identifiable, Hashable {
nonisolated func downloadImage() async throws
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 }
}
@@ -15,6 +15,8 @@ struct EmojiPackDetailView: View {
@State var editName: Bool = false
@State var editDescription: Bool = false
@State var downloading: Bool = false
@State var showShare: Bool = false
@State var useSwiftUIGrid: Bool = false
@@ -88,19 +90,6 @@ struct EmojiPackDetailView: View {
hoarder.saveEmojiPacks()
}
.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) {
Button(
"",
@@ -112,16 +101,31 @@ struct EmojiPackDetailView: View {
.tint(edit ? Color.accentColor : .primary)
}
ToolbarItem(placement: .topBarTrailing) {
Button("", systemImage: allDownloaded ? "checkmark" : "arrow.down") {
Task {
if allDownloaded {
await pack.deleteAll(hoarder: hoarder)
} else {
await pack.downloadAll(hoarder: hoarder)
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 {
if allDownloaded {
await pack.deleteAll(hoarder: hoarder)
} else {
await pack.downloadAll(hoarder: hoarder)
}
}
}
.tint(allDownloaded ? .red : .accentColor)
}
.tint(allDownloaded ? .red : .accentColor)
}
ToolbarItem(placement: .topBarTrailing) {
if #available(iOS 16, *) {
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.