Add svg pattern cached representation rendering

This commit is contained in:
Ilya Laktyushin 2019-12-28 02:32:15 +03:00
parent 56f5299cc9
commit e5312fb095
3 changed files with 49 additions and 36 deletions

View File

@ -4,6 +4,6 @@
#import <Foundation/Foundation.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 */

View File

@ -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);

View File

@ -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" {
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) {
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> {
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 == "<?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))
let intensity = CGFloat(representation.intensity) / 100.0
let colorImage = generateImage(size, contextGenerator: { size, c in
} 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)
c.setBlendMode(.copy)
@ -513,10 +529,10 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource,
}
c.setBlendMode(.normal)
if let cgImage = image.cgImage {
if let cgImage = maskImage?.cgImage {
c.clip(to: rect, mask: cgImage)
}
if colors.count == 1, let color = colors.first {
c.setFillColor(patternColor(for: color, intensity: intensity).cgColor)
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())
}
}, 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) {
CGImageDestinationSetProperties(colorDestination, [:] as CFDictionary)
let colorQuality: Float = 0.9
let options = NSMutableDictionary()
options.setObject(colorQuality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
CGImageDestinationAddImage(colorDestination, colorImage.cgImage!, options as CFDictionary)
if CGImageDestinationFinalize(colorDestination) {
subscriber.putNext(.temporaryPath(path))
subscriber.putCompletion()
}
let colorQuality: Float = 0.9
let options = NSMutableDictionary()
options.setObject(colorQuality as NSNumber, forKey: kCGImageDestinationLossyCompressionQuality as NSString)
CGImageDestinationAddImage(colorDestination, colorImage.cgImage!, options as CFDictionary)
if CGImageDestinationFinalize(colorDestination) {
subscriber.putNext(.temporaryPath(path))
subscriber.putCompletion()
}
}
}