mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-04 21:41:45 +00:00
Add svg pattern cached representation rendering
This commit is contained in:
parent
56f5299cc9
commit
e5312fb095
@ -4,6 +4,6 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size);
|
UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size, UIColor *backgroundColor, UIColor *foregroundColor);
|
||||||
|
|
||||||
#endif /* Lottie_h */
|
#endif /* Lottie_h */
|
||||||
|
|||||||
@ -83,7 +83,7 @@ CGSize aspectFillSize(CGSize size, CGSize bounds) {
|
|||||||
|
|
||||||
@end
|
@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];
|
NSDate *startTime = [NSDate date];
|
||||||
|
|
||||||
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
|
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
|
||||||
@ -116,9 +116,6 @@ UIImage * _Nullable drawSvgImage(NSData * _Nonnull data, CGSize size) {
|
|||||||
|
|
||||||
startTime = [NSDate date];
|
startTime = [NSDate date];
|
||||||
|
|
||||||
UIColor *backgroundColor = [UIColor blackColor];
|
|
||||||
UIColor *foregroundColor = [UIColor whiteColor];
|
|
||||||
|
|
||||||
UIGraphicsBeginImageContextWithOptions(size, true, 1.0);
|
UIGraphicsBeginImageContextWithOptions(size, true, 1.0);
|
||||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||||
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
|
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
|
||||||
|
|||||||
@ -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 == "<?xml" {
|
if data.count > 5, let string = String(data: data.subdata(in: 0 ..< 5), encoding: .utf8), string == "<?xml" {
|
||||||
let size = representation.size ?? CGSize(width: 1440.0, height: 2960.0)
|
let size = representation.size ?? CGSize(width: 1440.0, height: 2960.0)
|
||||||
|
|
||||||
if let image = drawSvgImage(data, size) {
|
if let image = drawSvgImage(data, size, .black, .white) {
|
||||||
if let alphaDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeJPEG, 1, nil) {
|
if let alphaDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeJPEG, 1, nil) {
|
||||||
CGImageDestinationSetProperties(alphaDestination, [:] as CFDictionary)
|
CGImageDestinationSetProperties(alphaDestination, [:] as CFDictionary)
|
||||||
|
|
||||||
@ -470,13 +470,14 @@ private func fetchCachedPatternWallpaperMaskRepresentation(resource: MediaResour
|
|||||||
|
|
||||||
private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, resourceData: MediaResourceData, representation: CachedPatternWallpaperRepresentation) -> Signal<CachedMediaResourceRepresentationResult, NoError> {
|
private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, resourceData: MediaResourceData, representation: CachedPatternWallpaperRepresentation) -> Signal<CachedMediaResourceRepresentationResult, NoError> {
|
||||||
return Signal({ subscriber in
|
return Signal({ subscriber in
|
||||||
if let data = try? Data(contentsOf: URL(fileURLWithPath: resourceData.path), options: [.mappedIfSafe]) {
|
if var data = try? Data(contentsOf: URL(fileURLWithPath: resourceData.path), options: [.mappedIfSafe]) {
|
||||||
if let image = UIImage(data: data) {
|
if let unzippedData = TGGUnzipData(data, 2 * 1024 * 1024) {
|
||||||
|
data = unzippedData
|
||||||
|
}
|
||||||
|
|
||||||
let path = NSTemporaryDirectory() + "\(arc4random64())"
|
let path = NSTemporaryDirectory() + "\(arc4random64())"
|
||||||
let url = URL(fileURLWithPath: path)
|
let url = URL(fileURLWithPath: path)
|
||||||
|
|
||||||
let size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
|
|
||||||
|
|
||||||
var colors: [UIColor] = []
|
var colors: [UIColor] = []
|
||||||
if let bottomColor = representation.bottomColor {
|
if let bottomColor = representation.bottomColor {
|
||||||
colors.append(UIColor(rgb: bottomColor))
|
colors.append(UIColor(rgb: bottomColor))
|
||||||
@ -485,7 +486,22 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource,
|
|||||||
|
|
||||||
let intensity = CGFloat(representation.intensity) / 100.0
|
let intensity = CGFloat(representation.intensity) / 100.0
|
||||||
|
|
||||||
let colorImage = generateImage(size, contextGenerator: { size, c in
|
var size: CGSize?
|
||||||
|
var maskImage: UIImage?
|
||||||
|
if data.count > 5, let string = String(data: data.subdata(in: 0 ..< 5), encoding: .utf8), string == "<?xml" {
|
||||||
|
let defaultSize = CGSize(width: 1440.0, height: 2960.0)
|
||||||
|
size = defaultSize
|
||||||
|
if let image = drawSvgImage(data, defaultSize, .black, .white) {
|
||||||
|
maskImage = image
|
||||||
|
}
|
||||||
|
} else if let image = UIImage(data: data) {
|
||||||
|
size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
|
||||||
|
maskImage = image
|
||||||
|
}
|
||||||
|
|
||||||
|
var colorImage: UIImage?
|
||||||
|
if let size = size {
|
||||||
|
colorImage = generateImage(size, contextGenerator: { size, c in
|
||||||
let rect = CGRect(origin: CGPoint(), size: size)
|
let rect = CGRect(origin: CGPoint(), size: size)
|
||||||
c.setBlendMode(.copy)
|
c.setBlendMode(.copy)
|
||||||
|
|
||||||
@ -513,7 +529,7 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource,
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.setBlendMode(.normal)
|
c.setBlendMode(.normal)
|
||||||
if let cgImage = image.cgImage {
|
if let cgImage = maskImage?.cgImage {
|
||||||
c.clip(to: rect, mask: cgImage)
|
c.clip(to: rect, mask: cgImage)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -538,6 +554,7 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource,
|
|||||||
c.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: rect.height), options: CGGradientDrawingOptions())
|
c.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: rect.height), options: CGGradientDrawingOptions())
|
||||||
}
|
}
|
||||||
}, scale: 1.0)
|
}, scale: 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
if let colorImage = colorImage, let colorDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeJPEG, 1, nil) {
|
if let colorImage = colorImage, let colorDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeJPEG, 1, nil) {
|
||||||
CGImageDestinationSetProperties(colorDestination, [:] as CFDictionary)
|
CGImageDestinationSetProperties(colorDestination, [:] as CFDictionary)
|
||||||
@ -554,7 +571,6 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return EmptyDisposable
|
return EmptyDisposable
|
||||||
}) |> runOn(Queue.concurrentDefaultQueue())
|
}) |> runOn(Queue.concurrentDefaultQueue())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user