mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-29 00:50:40 +00:00
57 lines
2.8 KiB
Swift
57 lines
2.8 KiB
Swift
import Foundation
|
|
import SwiftSignalKit
|
|
import Postbox
|
|
import Display
|
|
import ImageIO
|
|
import TelegramCore
|
|
|
|
private let roundCorners = { () -> UIImage in
|
|
let diameter: CGFloat = 60.0
|
|
UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0.0)
|
|
let context = UIGraphicsGetCurrentContext()!
|
|
context.setBlendMode(.copy)
|
|
context.setFillColor(UIColor.black.cgColor)
|
|
context.fill(CGRect(origin: CGPoint(), size: CGSize(width: diameter, height: diameter)))
|
|
context.setFillColor(UIColor.clear.cgColor)
|
|
context.fillEllipse(in: CGRect(origin: CGPoint(), size: CGSize(width: diameter, height: diameter)))
|
|
let image = UIGraphicsGetImageFromCurrentImageContext()!.stretchableImage(withLeftCapWidth: Int(diameter / 2.0), topCapHeight: Int(diameter / 2.0))
|
|
UIGraphicsEndImageContext()
|
|
return image
|
|
}()
|
|
|
|
func peerAvatarImage(account: Account, peer: Peer, displayDimensions: CGSize = CGSize(width: 60.0, height: 60.0)) -> Signal<UIImage, NoError>? {
|
|
if let location = peer.smallProfileImage?.location.cloudLocation {
|
|
return deferred { () -> Signal<UIImage, NoError> in
|
|
return cachedCloudFileLocation(location)
|
|
|> `catch` { _ in
|
|
return multipartDownloadFromCloudLocation(account: account, location: location, size: nil)
|
|
|> afterNext { data in
|
|
cacheCloudFileLocation(location, data: data)
|
|
}
|
|
}
|
|
|> runOn(account.graphicsThreadPool) |> deliverOn(account.graphicsThreadPool)
|
|
|> map { data -> UIImage in
|
|
assertNotOnMainThread()
|
|
|
|
if let image = generateImage(displayDimensions, contextGenerator: { size, context -> Void in
|
|
if let imageSource = CGImageSourceCreateWithData(data as CFData, nil), let dataImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil) {
|
|
context.setBlendMode(.copy)
|
|
context.draw(dataImage, in: CGRect(origin: CGPoint(), size: displayDimensions))
|
|
context.setBlendMode(.destinationOut)
|
|
context.draw(roundCorners.cgImage!, in: CGRect(origin: CGPoint(), size: displayDimensions))
|
|
}
|
|
}) {
|
|
return image
|
|
} else {
|
|
UIGraphicsBeginImageContextWithOptions(displayDimensions, false, 0.0)
|
|
let image = UIGraphicsGetImageFromCurrentImageContext()!
|
|
UIGraphicsEndImageContext()
|
|
return image
|
|
}
|
|
}
|
|
} |> runOn(account.graphicsThreadPool)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|