mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
106 lines
5.5 KiB
Swift
106 lines
5.5 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, cached: Bool = true) -> 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 cached && composed && wallpaper == backgroundImageForWallpaper?.0, (wallpaper.settings?.blur ?? false) == backgroundImageForWallpaper?.1 {
|
|
backgroundImage = backgroundImageForWallpaper?.2
|
|
} else {
|
|
var succeed = true
|
|
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(argb: color).withAlphaComponent(1.0).cgColor)
|
|
context.fill(CGRect(origin: CGPoint(), size: size))
|
|
})
|
|
case let .gradient(topColor, bottomColor, settings):
|
|
backgroundImage = generateImage(CGSize(width: 640.0, height: 1280.0), rotatedContext: { size, context in
|
|
let gradientColors = [UIColor(argb: topColor).cgColor, UIColor(argb: bottomColor).cgColor] as CFArray
|
|
|
|
var locations: [CGFloat] = [0.0, 1.0]
|
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
|
let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors, locations: &locations)!
|
|
|
|
context.translateBy(x: 320.0, y: 640.0)
|
|
context.rotate(by: CGFloat(settings.rotation ?? 0) * CGFloat.pi / 180.0)
|
|
context.translateBy(x: -320.0, y: -640.0)
|
|
|
|
context.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: size.height), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
|
|
})
|
|
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) {
|
|
succeed = false
|
|
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, bottomColor: file.settings.bottomColor, intensity: intensity, rotation: file.settings.rotation), 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) {
|
|
succeed = false
|
|
backgroundImage = UIImage(contentsOfFile: path)?.precomposed()
|
|
}
|
|
}
|
|
}
|
|
if let backgroundImage = backgroundImage, composed && succeed {
|
|
backgroundImageForWallpaper = (wallpaper, (wallpaper.settings?.blur ?? false), backgroundImage)
|
|
}
|
|
}
|
|
return backgroundImage
|
|
}
|