mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-02 00:17:02 +00:00

# Conflicts: # submodules/TelegramPresentationData/Sources/ComponentsThemes.swift # submodules/TelegramUI/TelegramUI/ChatController.swift # submodules/TelegramUI/TelegramUI/NavigateToChatController.swift
72 lines
3.8 KiB
Swift
72 lines
3.8 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import AsyncDisplayKit
|
|
import TelegramCore
|
|
import Display
|
|
import SwiftSignalKit
|
|
import Postbox
|
|
|
|
|
|
private var backgroundImageForWallpaper: (TelegramWallpaper, Bool, UIImage)?
|
|
|
|
func chatControllerBackgroundImage(wallpaper: TelegramWallpaper, mediaBox: MediaBox, composed: Bool = true) -> UIImage? {
|
|
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 = frameworkBundle.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)).cgColor)
|
|
context.fill(CGRect(origin: CGPoint(), size: size))
|
|
})
|
|
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
|
|
}
|