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
+85
-74
No files matched your search
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
|
|
||||||
static func dataFrom(gif: CGImage) -> Data? {
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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)
|
context.interpolationQuality = .default
|
||||||
|
context.draw(cgImage, in: CGRect(origin: .zero, size: targetSize))
|
||||||
if let frames = image.images {
|
return context.makeImage()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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,16 +101,31 @@ 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 {
|
||||||
Task {
|
ProgressView()
|
||||||
if allDownloaded {
|
.onChange(of: allDownloaded) { newValue in
|
||||||
await pack.deleteAll(hoarder: hoarder)
|
if newValue == true {
|
||||||
} else {
|
downloading = false
|
||||||
await pack.downloadAll(hoarder: hoarder)
|
}
|
||||||
|
}
|
||||||
|
} 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) {
|
ToolbarItem(placement: .topBarTrailing) {
|
||||||
if #available(iOS 16, *) {
|
if #available(iOS 16, *) {
|
||||||
|
|||||||