diff --git a/StickerSlack/Emoji/EmojiHoarder.swift b/StickerSlack/Emoji/EmojiHoarder.swift index 05de597..bbdea4e 100644 --- a/StickerSlack/Emoji/EmojiHoarder.swift +++ b/StickerSlack/Emoji/EmojiHoarder.swift @@ -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) - } } } } diff --git a/StickerSlack/Emoji/GifManager.swift b/StickerSlack/Emoji/GifManager.swift index 769f522..7dc4289 100644 --- a/StickerSlack/Emoji/GifManager.swift +++ b/StickerSlack/Emoji/GifManager.swift @@ -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 - } - - static func dataFrom(gif: CGImage) -> Data? { + 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 + ) + } + } + + guard CGImageDestinationFinalize(destination) else { fatalError() } + return data as Data } } diff --git a/StickerSlack/Gifs/Gif.swift b/StickerSlack/Gifs/Gif.swift index ee3d22e..30ccf1b 100644 --- a/StickerSlack/Gifs/Gif.swift +++ b/StickerSlack/Gifs/Gif.swift @@ -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) diff --git a/StickerSlack/Stickers/Sticker.swift b/StickerSlack/Stickers/Sticker.swift index 5ca0a47..48f3d44 100644 --- a/StickerSlack/Stickers/Sticker.swift +++ b/StickerSlack/Stickers/Sticker.swift @@ -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 + } + + 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() } - 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() + context.interpolationQuality = .default + context.draw(cgImage, in: CGRect(origin: .zero, size: targetSize)) + return context.makeImage() } } diff --git a/StickerSlack/Stickers/StickerProtocol.swift b/StickerSlack/Stickers/StickerProtocol.swift index a827c58..c922a0f 100644 --- a/StickerSlack/Stickers/StickerProtocol.swift +++ b/StickerSlack/Stickers/StickerProtocol.swift @@ -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 } } diff --git a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift index 80a9a22..aac433d 100644 --- a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift +++ b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift @@ -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, *) {