diff --git a/submodules/TelegramUI/Components/EmojiTextAttachmentView/Sources/EmojiTextAttachmentView.swift b/submodules/TelegramUI/Components/EmojiTextAttachmentView/Sources/EmojiTextAttachmentView.swift index e22da66af9..f0b417afcf 100644 --- a/submodules/TelegramUI/Components/EmojiTextAttachmentView/Sources/EmojiTextAttachmentView.swift +++ b/submodules/TelegramUI/Components/EmojiTextAttachmentView/Sources/EmojiTextAttachmentView.swift @@ -163,32 +163,51 @@ public final class InlineStickerItemLayer: MultiAnimationRenderTarget { } let context = self.context - self.disposable = renderer.add(groupId: self.groupId, target: self, cache: self.cache, itemId: file.resource.id.stringRepresentation, size: self.pixelSize, fetch: { size, writer in - let source = AnimatedStickerResourceSource(account: context.account, resource: file.resource, fitzModifier: nil, isVideo: false) - - let dataDisposable = source.directDataPath(attemptSynchronously: false).start(next: { result in - guard let result = result else { - return - } + if file.isAnimatedSticker || file.isVideoEmoji { + self.disposable = renderer.add(groupId: self.groupId, target: self, cache: self.cache, itemId: file.resource.id.stringRepresentation, size: self.pixelSize, fetch: { size, writer in + let source = AnimatedStickerResourceSource(account: context.account, resource: file.resource, fitzModifier: nil, isVideo: false) - if file.isVideoEmoji { - cacheVideoAnimation(path: result, width: Int(size.width), height: Int(size.height), writer: writer) - } else { - guard let data = try? Data(contentsOf: URL(fileURLWithPath: result)) else { - writer.finish() + let dataDisposable = source.directDataPath(attemptSynchronously: false).start(next: { result in + guard let result = result else { return } - cacheLottieAnimation(data: data, width: Int(size.width), height: Int(size.height), writer: writer) + + if file.isVideoEmoji { + cacheVideoAnimation(path: result, width: Int(size.width), height: Int(size.height), writer: writer) + } else { + guard let data = try? Data(contentsOf: URL(fileURLWithPath: result)) else { + writer.finish() + return + } + cacheLottieAnimation(data: data, width: Int(size.width), height: Int(size.height), writer: writer) + } + }) + + let fetchDisposable = freeMediaFileResourceInteractiveFetched(account: context.account, fileReference: .customEmoji(media: file), resource: file.resource).start() + + return ActionDisposable { + dataDisposable.dispose() + fetchDisposable.dispose() } }) - - let fetchDisposable = freeMediaFileResourceInteractiveFetched(account: context.account, fileReference: .customEmoji(media: file), resource: file.resource).start() - - return ActionDisposable { - dataDisposable.dispose() - fetchDisposable.dispose() - } - }) + } else { + self.disposable = renderer.add(groupId: self.groupId, target: self, cache: self.cache, itemId: file.resource.id.stringRepresentation, size: self.pixelSize, fetch: { size, writer in + let dataDisposable = context.account.postbox.mediaBox.resourceData(file.resource).start(next: { result in + guard result.complete else { + return + } + + cacheStillSticker(path: result.path, width: Int(size.width), height: Int(size.height), writer: writer) + }) + + let fetchDisposable = freeMediaFileResourceInteractiveFetched(account: context.account, fileReference: .customEmoji(media: file), resource: file.resource).start() + + return ActionDisposable { + dataDisposable.dispose() + fetchDisposable.dispose() + } + }) + } } } diff --git a/submodules/TelegramUI/Components/LottieAnimationCache/BUILD b/submodules/TelegramUI/Components/LottieAnimationCache/BUILD index 434120005d..15079a8090 100644 --- a/submodules/TelegramUI/Components/LottieAnimationCache/BUILD +++ b/submodules/TelegramUI/Components/LottieAnimationCache/BUILD @@ -15,6 +15,7 @@ swift_library( "//submodules/Display:Display", "//submodules/rlottie:RLottieBinding", "//submodules/GZip:GZip", + "//submodules/WebPBinding:WebPBinding", ], visibility = [ "//visibility:public", diff --git a/submodules/TelegramUI/Components/LottieAnimationCache/Sources/LottieAnimationCache.swift b/submodules/TelegramUI/Components/LottieAnimationCache/Sources/LottieAnimationCache.swift index 0a738c851f..85ef713466 100644 --- a/submodules/TelegramUI/Components/LottieAnimationCache/Sources/LottieAnimationCache.swift +++ b/submodules/TelegramUI/Components/LottieAnimationCache/Sources/LottieAnimationCache.swift @@ -4,6 +4,7 @@ import AnimationCache import Display import RLottieBinding import GZip +import WebPBinding public func cacheLottieAnimation(data: Data, width: Int, height: Int, writer: AnimationCacheItemWriter) { writer.queue.async { @@ -29,6 +30,18 @@ public func cacheLottieAnimation(data: Data, width: Int, height: Int, writer: An public func cacheStillSticker(path: String, width: Int, height: Int, writer: AnimationCacheItemWriter) { writer.queue.async { + if let data = try? Data(contentsOf: URL(fileURLWithPath: path)), let image = WebP.convert(fromWebP: data) { + writer.add(with: { surface in + let context = DrawingContext(size: CGSize(width: CGFloat(surface.width), height: CGFloat(surface.height)), scale: 1.0, opaque: false, clear: true, bytesPerRow: surface.bytesPerRow) + context.withContext { c in + UIGraphicsPushContext(c) + c.draw(image.cgImage!, in: CGRect(origin: CGPoint(), size: context.size)) + UIGraphicsPopContext() + } + memcpy(surface.argb, context.bytes, surface.height * surface.bytesPerRow) + }, proposedWidth: width, proposedHeight: height, duration: 1.0) + } + writer.finish() } }