From 0ba4de14575ee6bc90d6abfaa7748ce882aa219c Mon Sep 17 00:00:00 2001 From: Peter Iakovlev Date: Fri, 21 Dec 2018 16:44:35 +0400 Subject: [PATCH] Use minithumbs --- TelegramUI/ImageCompression.swift | 8 ++--- TelegramUI/PhotoResources.swift | 50 +++++++++++++++++++------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/TelegramUI/ImageCompression.swift b/TelegramUI/ImageCompression.swift index b7498c751b..36c3044819 100644 --- a/TelegramUI/ImageCompression.swift +++ b/TelegramUI/ImageCompression.swift @@ -207,7 +207,7 @@ func decompressTinyThumbnail(data: TinyThumbnailData) -> UIImage? { private let tinyThumbnailHeaderPattern = Data(base64Encoded: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDACgcHiMeGSgjISMtKygwPGRBPDc3PHtYXUlkkYCZlo+AjIqgtObDoKrarYqMyP/L2u71////m8H////6/+b9//j/2wBDASstLTw1PHZBQXb4pYyl+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj/wAARCAAAAAADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwA=") private let tinyThumbnailFooterPattern = Data(base64Encoded: "/9k=") -func decompressTinyThumbnail(data: Data) -> UIImage? { +func decodeTinyThumbnail(data: Data) -> Data? { if data.count < 3 { return nil } @@ -216,7 +216,7 @@ func decompressTinyThumbnail(data: Data) -> UIImage? { } var version: UInt8 = 0 data.copyBytes(to: &version, count: 1) - if version != 0 { + if version != 1 { return nil } var width: UInt8 = 0 @@ -229,10 +229,10 @@ func decompressTinyThumbnail(data: Data) -> UIImage? { resultData.append(data.subdata(in: 3 ..< data.count)) resultData.append(tinyThumbnailFooterPattern) resultData.withUnsafeMutableBytes({ (resultBytes: UnsafeMutablePointer) -> Void in - resultBytes[164] = width + resultBytes[164] = width - 3 resultBytes[166] = height }) - return UIImage(data: resultData) + return resultData } func serializeTinyThumbnail(_ data: TinyThumbnailData) -> String { diff --git a/TelegramUI/PhotoResources.swift b/TelegramUI/PhotoResources.swift index 291f6982cd..4f11768645 100644 --- a/TelegramUI/PhotoResources.swift +++ b/TelegramUI/PhotoResources.swift @@ -27,7 +27,13 @@ func chatMessagePhotoDatas(postbox: Postbox, photoReference: ImageMediaReference let loadedData: Data? = try? Data(contentsOf: URL(fileURLWithPath: maybeData.path), options: []) return .single((nil, loadedData, true)) } else { - let fetchedThumbnail = fetchedMediaResource(postbox: postbox, reference: photoReference.resourceReference(smallestRepresentation.resource), statsCategory: .image) + let decodedThumbnailData = photoReference.media.immediateThumbnailData.flatMap(decodeTinyThumbnail) + let fetchedThumbnail: Signal + if let _ = decodedThumbnailData { + fetchedThumbnail = .complete() + } else { + fetchedThumbnail = fetchedMediaResource(postbox: postbox, reference: photoReference.resourceReference(smallestRepresentation.resource), statsCategory: .image) + } let fetchedFullSize = fetchedMediaResource(postbox: postbox, reference: photoReference.resourceReference(largestRepresentation.resource), statsCategory: .image) let anyThumbnail: [Signal] @@ -43,14 +49,20 @@ func chatMessagePhotoDatas(postbox: Postbox, photoReference: ImageMediaReference } let mainThumbnail = Signal { subscriber in - let fetchedDisposable = fetchedThumbnail.start() - let thumbnailDisposable = postbox.mediaBox.resourceData(smallestRepresentation.resource, attemptSynchronously: synchronousLoad).start(next: { next in - subscriber.putNext(next.size == 0 ? nil : try? Data(contentsOf: URL(fileURLWithPath: next.path), options: [])) - }, error: subscriber.putError, completed: subscriber.putCompletion) - - return ActionDisposable { - fetchedDisposable.dispose() - thumbnailDisposable.dispose() + if let decodedThumbnailData = decodedThumbnailData { + subscriber.putNext(decodedThumbnailData) + subscriber.putCompletion() + return EmptyDisposable + } else { + let fetchedDisposable = fetchedThumbnail.start() + let thumbnailDisposable = postbox.mediaBox.resourceData(smallestRepresentation.resource, attemptSynchronously: synchronousLoad).start(next: { next in + subscriber.putNext(next.size == 0 ? nil : try? Data(contentsOf: URL(fileURLWithPath: next.path), options: [])) + }, error: subscriber.putError, completed: subscriber.putCompletion) + + return ActionDisposable { + fetchedDisposable.dispose() + thumbnailDisposable.dispose() + } } } @@ -587,17 +599,17 @@ private func addCorners(_ context: DrawingContext, arguments: TransformImageArgu func rawMessagePhoto(postbox: Postbox, photoReference: ImageMediaReference) -> Signal { return chatMessagePhotoDatas(postbox: postbox, photoReference: photoReference, autoFetchFullSize: true) - |> map { (thumbnailData, fullSizeData, fullSizeComplete) -> UIImage? in - if let fullSizeData = fullSizeData { - if fullSizeComplete { - return UIImage(data: fullSizeData)?.precomposed() - } + |> map { (thumbnailData, fullSizeData, fullSizeComplete) -> UIImage? in + if let fullSizeData = fullSizeData { + if fullSizeComplete { + return UIImage(data: fullSizeData)?.precomposed() } - if let thumbnailData = thumbnailData { - return UIImage(data: thumbnailData)?.precomposed() - } - return nil } + if let thumbnailData = thumbnailData { + return UIImage(data: thumbnailData)?.precomposed() + } + return nil + } } public func chatMessagePhoto(postbox: Postbox, photoReference: ImageMediaReference, synchronousLoad: Bool = false, tinyThumbnailData: TinyThumbnailData? = nil) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> { @@ -681,7 +693,7 @@ public func chatMessagePhotoInternal(photoData: Signal<(Data?, Data?, Bool), NoE c.interpolationQuality = .none c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) } - telegramFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) + //telegramFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5)) if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 {