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,22 +470,38 @@ 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) {
|
||||||
let path = NSTemporaryDirectory() + "\(arc4random64())"
|
data = unzippedData
|
||||||
let url = URL(fileURLWithPath: path)
|
}
|
||||||
|
|
||||||
let size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
|
let path = NSTemporaryDirectory() + "\(arc4random64())"
|
||||||
|
let url = URL(fileURLWithPath: path)
|
||||||
var colors: [UIColor] = []
|
|
||||||
if let bottomColor = representation.bottomColor {
|
var colors: [UIColor] = []
|
||||||
colors.append(UIColor(rgb: bottomColor))
|
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 == "<?xml" {
|
||||||
|
let defaultSize = CGSize(width: 1440.0, height: 2960.0)
|
||||||
|
size = defaultSize
|
||||||
|
if let image = drawSvgImage(data, defaultSize, .black, .white) {
|
||||||
|
maskImage = image
|
||||||
}
|
}
|
||||||
colors.append(UIColor(rgb: representation.color))
|
} else if let image = UIImage(data: data) {
|
||||||
|
size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
|
||||||
let intensity = CGFloat(representation.intensity) / 100.0
|
maskImage = image
|
||||||
|
}
|
||||||
let colorImage = generateImage(size, contextGenerator: { size, c in
|
|
||||||
|
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,10 +529,10 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if colors.count == 1, let color = colors.first {
|
if colors.count == 1, let color = colors.first {
|
||||||
c.setFillColor(patternColor(for: color, intensity: intensity).cgColor)
|
c.setFillColor(patternColor(for: color, intensity: intensity).cgColor)
|
||||||
c.fill(rect)
|
c.fill(rect)
|
||||||
@ -538,20 +554,20 @@ 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) {
|
||||||
|
CGImageDestinationSetProperties(colorDestination, [:] as CFDictionary)
|
||||||
|
|
||||||
if let colorImage = colorImage, let colorDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeJPEG, 1, nil) {
|
let colorQuality: Float = 0.9
|
||||||
CGImageDestinationSetProperties(colorDestination, [:] as CFDictionary)
|
|
||||||
|
let options = NSMutableDictionary()
|
||||||
let colorQuality: Float = 0.9
|
options.setObject(colorQuality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
|
||||||
|
|
||||||
let options = NSMutableDictionary()
|
CGImageDestinationAddImage(colorDestination, colorImage.cgImage!, options as CFDictionary)
|
||||||
options.setObject(colorQuality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
|
if CGImageDestinationFinalize(colorDestination) {
|
||||||
|
subscriber.putNext(.temporaryPath(path))
|
||||||
CGImageDestinationAddImage(colorDestination, colorImage.cgImage!, options as CFDictionary)
|
subscriber.putCompletion()
|
||||||
if CGImageDestinationFinalize(colorDestination) {
|
|
||||||
subscriber.putNext(.temporaryPath(path))
|
|
||||||
subscriber.putCompletion()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user