mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-07 16:11:13 +00:00
Use minithumbs
This commit is contained in:
parent
be6d8b34a8
commit
0ba4de1457
@ -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<UInt8>) -> Void in
|
||||
resultBytes[164] = width
|
||||
resultBytes[164] = width - 3
|
||||
resultBytes[166] = height
|
||||
})
|
||||
return UIImage(data: resultData)
|
||||
return resultData
|
||||
}
|
||||
|
||||
func serializeTinyThumbnail(_ data: TinyThumbnailData) -> String {
|
||||
|
@ -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<FetchResourceSourceType, NoError>
|
||||
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<MediaResourceData, NoError>]
|
||||
@ -43,14 +49,20 @@ func chatMessagePhotoDatas(postbox: Postbox, photoReference: ImageMediaReference
|
||||
}
|
||||
|
||||
let mainThumbnail = Signal<Data?, NoError> { 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<UIImage?, NoError> {
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user