mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
99 lines
5.1 KiB
Swift
99 lines
5.1 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import AsyncDisplayKit
|
|
import TelegramCore
|
|
import SyncCore
|
|
import Display
|
|
import SwiftSignalKit
|
|
import Postbox
|
|
import MediaResources
|
|
import AppBundle
|
|
|
|
private var backgroundImageForWallpaper: (TelegramWallpaper, Bool, UIImage)?
|
|
|
|
public func chatControllerBackgroundImage(theme: PresentationTheme?, wallpaper initialWallpaper: TelegramWallpaper, mediaBox: MediaBox, composed: Bool = true, knockoutMode: Bool) -> UIImage? {
|
|
var wallpaper = initialWallpaper
|
|
if knockoutMode, let theme = theme {
|
|
switch theme.name {
|
|
case let .builtin(name):
|
|
switch name {
|
|
case .day, .night, .nightAccent:
|
|
wallpaper = theme.chat.defaultWallpaper
|
|
case .dayClassic:
|
|
break
|
|
}
|
|
case .custom:
|
|
break
|
|
}
|
|
}
|
|
|
|
var backgroundImage: UIImage?
|
|
if composed && wallpaper == backgroundImageForWallpaper?.0, (wallpaper.settings?.blur ?? false) == backgroundImageForWallpaper?.1 {
|
|
backgroundImage = backgroundImageForWallpaper?.2
|
|
} else {
|
|
switch wallpaper {
|
|
case .builtin:
|
|
if let filePath = getAppBundle().path(forResource: "ChatWallpaperBuiltin0", ofType: "jpg") {
|
|
backgroundImage = UIImage(contentsOfFile: filePath)?.precomposed()
|
|
}
|
|
case let .color(color):
|
|
backgroundImage = generateImage(CGSize(width: 1.0, height: 1.0), rotatedContext: { size, context in
|
|
context.setFillColor(UIColor(rgb: UInt32(bitPattern: color)).withAlphaComponent(1.0).cgColor)
|
|
context.fill(CGRect(origin: CGPoint(), size: size))
|
|
})
|
|
case let .gradient(topColor, bottomColor):
|
|
backgroundImage = generateImage(CGSize(width: 1.0, height: 1280.0), rotatedContext: { size, context in
|
|
let gradientColors = [UIColor(rgb: UInt32(bitPattern: topColor)).cgColor, UIColor(rgb: UInt32(bitPattern: bottomColor)).cgColor] as CFArray
|
|
|
|
var locations: [CGFloat] = [0.0, 1.0]
|
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
|
let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors, locations: &locations)!
|
|
|
|
context.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: size.height), options: CGGradientDrawingOptions())
|
|
})
|
|
case let .image(representations, settings):
|
|
if let largest = largestImageRepresentation(representations) {
|
|
if settings.blur && composed {
|
|
var image: UIImage?
|
|
let _ = mediaBox.cachedResourceRepresentation(largest.resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true, attemptSynchronously: true).start(next: { data in
|
|
if data.complete {
|
|
image = UIImage(contentsOfFile: data.path)?.precomposed()
|
|
}
|
|
})
|
|
backgroundImage = image
|
|
}
|
|
if backgroundImage == nil, let path = mediaBox.completedResourcePath(largest.resource) {
|
|
backgroundImage = UIImage(contentsOfFile: path)?.precomposed()
|
|
}
|
|
}
|
|
case let .file(file):
|
|
if file.isPattern, let color = file.settings.color, let intensity = file.settings.intensity {
|
|
var image: UIImage?
|
|
let _ = mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(color: color, intensity: intensity), complete: true, fetch: true, attemptSynchronously: true).start(next: { data in
|
|
if data.complete {
|
|
image = UIImage(contentsOfFile: data.path)?.precomposed()
|
|
}
|
|
})
|
|
backgroundImage = image
|
|
} else {
|
|
if file.settings.blur && composed {
|
|
var image: UIImage?
|
|
let _ = mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true, attemptSynchronously: true).start(next: { data in
|
|
if data.complete {
|
|
image = UIImage(contentsOfFile: data.path)?.precomposed()
|
|
}
|
|
})
|
|
backgroundImage = image
|
|
}
|
|
if backgroundImage == nil, let path = mediaBox.completedResourcePath(file.file.resource) {
|
|
backgroundImage = UIImage(contentsOfFile: path)?.precomposed()
|
|
}
|
|
}
|
|
}
|
|
if let backgroundImage = backgroundImage, composed {
|
|
backgroundImageForWallpaper = (wallpaper, (wallpaper.settings?.blur ?? false), backgroundImage)
|
|
}
|
|
}
|
|
return backgroundImage
|
|
}
|