mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
rewrote resize funciton
This commit is contained in:
6 files changed
+77
-66
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
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? {
|
||||
|
||||
return nil
|
||||
guard CGImageDestinationFinalize(destination) else { fatalError() }
|
||||
return data as Data
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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,7 +101,21 @@ struct EmojiPackDetailView: View {
|
||||
.tint(edit ? Color.accentColor : .primary)
|
||||
}
|
||||
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 {
|
||||
if allDownloaded {
|
||||
await pack.deleteAll(hoarder: hoarder)
|
||||
@@ -123,6 +126,7 @@ struct EmojiPackDetailView: View {
|
||||
}
|
||||
.tint(allDownloaded ? .red : .accentColor)
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
if #available(iOS 16, *) {
|
||||
ShareLink(item: pack.shareLink()) {
|
||||
|
||||