From e5312fb095bc175d19145f7e1ac8cb0eedd89d85 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 28 Dec 2019 02:32:15 +0300 Subject: [PATCH] Add svg pattern cached representation rendering --- submodules/Svg/Sources/Svg.h | 2 +- submodules/Svg/Sources/Svg.m | 5 +- .../FetchCachedRepresentations.swift | 78 +++++++++++-------- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/submodules/Svg/Sources/Svg.h b/submodules/Svg/Sources/Svg.h index 69063f40a5..440279afcf 100755 --- a/submodules/Svg/Sources/Svg.h +++ b/submodules/Svg/Sources/Svg.h @@ -4,6 +4,6 @@ #import #import -UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size); +UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size, UIColor *backgroundColor, UIColor *foregroundColor); #endif /* Lottie_h */ diff --git a/submodules/Svg/Sources/Svg.m b/submodules/Svg/Sources/Svg.m index 9e62bd1d5f..def1d2b873 100755 --- a/submodules/Svg/Sources/Svg.m +++ b/submodules/Svg/Sources/Svg.m @@ -83,7 +83,7 @@ CGSize aspectFillSize(CGSize size, CGSize bounds) { @end -UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size) { +UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size, UIColor *backgroundColor, UIColor *foregroundColor) { NSDate *startTime = [NSDate date]; NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; @@ -116,9 +116,6 @@ UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size) { startTime = [NSDate date]; - UIColor *backgroundColor = [UIColor blackColor]; - UIColor *foregroundColor = [UIColor whiteColor]; - UIGraphicsBeginImageContextWithOptions(size, true, 1.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, backgroundColor.CGColor); diff --git a/submodules/TelegramUI/TelegramUI/FetchCachedRepresentations.swift b/submodules/TelegramUI/TelegramUI/FetchCachedRepresentations.swift index 1f7b5607f5..99e8bb086a 100644 --- a/submodules/TelegramUI/TelegramUI/FetchCachedRepresentations.swift +++ b/submodules/TelegramUI/TelegramUI/FetchCachedRepresentations.swift @@ -421,7 +421,7 @@ private func fetchCachedPatternWallpaperMaskRepresentation(resource: MediaResour if data.count > 5, let string = String(data: data.subdata(in: 0 ..< 5), encoding: .utf8), string == " Signal { return Signal({ subscriber in - if let data = try? Data(contentsOf: URL(fileURLWithPath: resourceData.path), options: [.mappedIfSafe]) { - if let image = UIImage(data: data) { - let path = NSTemporaryDirectory() + "\(arc4random64())" - let url = URL(fileURLWithPath: path) - - let size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale) - - var colors: [UIColor] = [] - if let bottomColor = representation.bottomColor { - colors.append(UIColor(rgb: bottomColor)) + if var data = try? Data(contentsOf: URL(fileURLWithPath: resourceData.path), options: [.mappedIfSafe]) { + if let unzippedData = TGGUnzipData(data, 2 * 1024 * 1024) { + data = unzippedData + } + + let path = NSTemporaryDirectory() + "\(arc4random64())" + let url = URL(fileURLWithPath: path) + + var colors: [UIColor] = [] + if let bottomColor = representation.bottomColor { + colors.append(UIColor(rgb: bottomColor)) + } + colors.append(UIColor(rgb: representation.color)) + + let intensity = CGFloat(representation.intensity) / 100.0 + + var size: CGSize? + var maskImage: UIImage? + if data.count > 5, let string = String(data: data.subdata(in: 0 ..< 5), encoding: .utf8), string == "