class StackedDrawables(layers: Array, private val iconScale: Double = 0.70) :
LayerDrawable(layers) {
private var shouldPlaceIcons = true
companion object {
fun fromDeviceTypes(deviceTypes: List, context: Context): StackedDrawables? {
val firstTwo = deviceTypes.chunked(2).firstOrNull()?.toArrayList() ?: kotlin.run {
return null
}
if (firstTwo.size == 1) firstTwo.add(firstTwo.first())
val drawableList = firstTwo.mapNotNull {
context.getDrawable(it.iconOutlinedImageResource)
}.toArrayList().reversed().toTypedArray()
return StackedDrawables(drawableList)
}
}
override fun onBoundsChange(bounds: Rect) {
super.onBoundsChange(bounds)
if (shouldPlaceIcons) placeIcons() else shouldPlaceIcons = true
}
private fun placeIcons() {
shouldPlaceIcons = false
val newWidth = copyBounds().width()
val iconsSize = floor(newWidth.times(iconScale)).roundToInt()
val iconsInset =
(newWidth - iconsSize) - 1 // Need to subtract 1, otherwise it doesn't work for some reason...
for (index in 0 until numberOfLayers) {
setLayerSize(index, iconsSize, iconsSize)
setLayerGravity(index, Gravity.CENTER)
when (index) {
1 -> setLayerInset(index, 0, iconsInset, iconsInset, 0)
0 -> setLayerInset(index, iconsInset, 0, 0, iconsInset)
}
}
invalidateSelf()
}
}
Assistant
This class is an extension of the LayerDrawable class in the Android framework. It is used to stack multiple Drawable objects on top of each other to create a composite image. The StackedDrawables class takes an array of Drawable objects and an optional iconScale parameter (defaults to 0.70) in its constructor, and overrides the onBoundsChange method to position the drawables correctly within the bounds of the container. The class also contains a companion object method fromDeviceTypes(deviceTypes: List<DeviceType>, context: Context) which will take a List of DeviceType and context, it takes the first two elements of the list, it maps the iconOutlinedImageResource of the DeviceType to drawable and reverse the order of the Drawable and return the StackedDrawable with the drawable array.
User
Ok, can you create a subclass of UIImage, called StackedUIImage, on an iOS Swift app that achieves the same result
Assistant
Sure! Here's an example of how you could create a StackedUIImage subclass in an iOS Swift app that functions similarly to the StackedDrawables class you provided:
code
import UIKit
class StackedUIImage: UIImage {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
override func draw(in rect: CGRect) {
super.draw(in: rect)
let newWidth = bounds.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
for (index, layer) in layers.enumerated() {
let imageRect = CGRect(x: iconsInset, y: iconsInset, width: iconsSize, height: iconsSize)
layer.draw(in: imageRect)
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedUIImage? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImage = StackedUIImage()
stackedImage.layers = imageList.reversed()
return stackedImage
}
}
Here, StackedUIImage is a subclass of UIImage, it has two properties iconScale and layers, the draw function will be called when the image is rendered on the screen, it will draw each image in the layers array in the given rect, the image will be scaled according to the iconScale property and the fromDeviceTypes is a class method that takes a list of DeviceType and a context, it takes the first two elements of the list, maps the iconOutlinedImageResource of the DeviceType to UIImage, reverse the order of the images and return the StackedUIImage with the layers set to the reversed images.
User
can you describe to me what the result image will look like?
Assistant
The result image will be a composite image made up of multiple layers of images stacked on top of each other. The position of each image within the composite will be determined by the placeIcons() method or the draw(in rect: CGRect) method in the StackedDrawables and StackedUIImage respectively.
The size of each image will be scaled down by a factor of iconScale before being drawn to the composite image, with the default value being 0.7. The images will be drawn in the center of the composite image and will be placed in a reversed order.
For example, if the layers array contains the images [A, B, C], the final result image will be composed of image C at the bottom layer, image B on top of it, and image A on the topmost layer.
User
for an array of images [A, B, C] it should actually render A on the bottom left corner, on top of B. Also, B should be placed at the top right corner, behind A. Any other images on the array should be ignored. Can you update the iOS code to do that?
Assistant
Sure! Here's an example of how you could update the StackedUIImage class in an iOS Swift app to achieve the desired result:
code
import UIKit
class StackedUIImage: UIImage {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
override func draw(in rect: CGRect) {
super.draw(in: rect)
let newWidth = bounds.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
if layers.count > 1 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
if layers.count > 0 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedUIImage? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImage = StackedUIImage()
stackedImage.layers = imageList
return stackedImage
}
}
As you can see I've added an if statement for each draw call, so it only draws two images regardless of the size of the layers array. Also I've removed the reversed function from the fromDeviceTypes method. The first image of the array will be drawn in the bottom left corner, and the second image will be drawn on top of the first image, in the top right corner.
User
The second image should be behind the first image
Assistant
I apologize for the confusion, to make the second image to be behind the first image, you would need to draw the second image first and then the first image, this way the first image will be drawn on top of the second image. Here's an example of how you could update the StackedUIImage class in an iOS Swift app to achieve the desired result:
code
import UIKit
class StackedUIImage: UIImage {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
override func draw(in rect: CGRect) {
super.draw(in: rect)
let newWidth = bounds.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
if layers.count > 1 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
if layers.count > 0 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedUIImage? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImage = StackedUIImage()
stackedImage.layers = imageList
return stackedImage
}
}
Now the second image will be drawn first, and the first image will be drawn on top of it, so the first image will appear in front of the second image.
User
UIImage doesn't have bounds, can I just use the rect passed on the draw method instead?
Assistant
Yes, you can use the rect passed to the draw(in rect: CGRect) method instead of bounds to determine the size of the image. The rect parameter is the area in which the image should be drawn and it will contain the size of the container in which the image is being rendered.
Here's an example of how you could update the StackedUIImage class to use the rect parameter instead of bounds:
code
import UIKit
class StackedUIImage: UIImage {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
override func draw(in rect: CGRect) {
super.draw(in: rect)
let newWidth = rect.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
if layers.count > 1 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
if layers.count > 0 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedUIImage? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImage = StackedUIImage()
stackedImage.layers = imageList
return stackedImage
}
}
This way the StackedUIImage class will correctly use the size of the container in which the image is being rendered to position and scale the images.
User
I've tried that code but the image is not being rendered. Can you change it to a helper class, that doesn't extend UIImage, but generates one?
Assistant
Sure! Here's an example of how you could create a helper class in iOS Swift that generates a composite image similar to the StackedUIImage class:
code
import UIKit
class StackedImageHelper {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
func generateImage() -> UIImage {
let size = calculateSize()
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { (context) in
let newWidth = size.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
if layers.count > 1 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
if layers.count > 0 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedImageHelper? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImageHelper = StackedImageHelper()
stackedImageHelper.layers = imageList
return stackedImageHelper
}
private func calculateSize() -> CGSize {
let width = max(layers[0].size.width, layers[1].size.width)
let height = max(layers[0].size.height, layers[1].size.height)
return CGSize(width: width, height: height)
}
}
This helper class provides a generateImage() function which when called will return a new UIImage containing the composite image. It also has fromDeviceTypes static function that takes a list of DeviceType and a context, it takes the first two elements of the list, maps the iconOutlinedImageResource of the DeviceType to UIImage, and returns the helper object with the layers set to the images.
You can use the helper class like this:
code
let helper = StackedImageHelper.fromDeviceTypes(deviceTypes: devices, context: self.view)
let image = helper?.generateImage()
It creates an instance of the helper class and set the layers using the devices, then calls the generateImage method to create the final UIImage.
User
ok, but the size of the generated image should be the same as the UIImageView
Assistant
You are correct, in that case, the size of the generated image should match the size of the UIImageView where it will be displayed. You can pass the size of the UIImageView to the StackedImageHelper class, and use that size in the generateImage() method to correctly size the composite image.
Here's an example of how you could update the StackedImageHelper class to take the size of the UIImageView as a parameter:
code
import UIKit
class StackedImageHelper {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
var imageViewSize: CGSize
init(imageViewSize: CGSize) {
self.imageViewSize = imageViewSize
}
func generateImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(size: imageViewSize)
return renderer.image { (context) in
let newWidth = imageViewSize.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
User
continue
Assistant
Sure, here's the rest of the updated generateImage() method:
code
if layers.count > 1 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
if layers.count > 0 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
}
}
// ...
}
The generateImage() method now uses the imageViewSize property to set the size of the renderer and the size of the final composite image. To create an instance of the StackedImageHelper class you would now pass the size of the UIImageView as an argument:
code
let helper = StackedImageHelper(imageViewSize: myImageView.frame.size)
let image = helper.generateImage()
You can then set the image to your image view.
User
what is the // ...?
Assistant
The // ... is a placeholder that indicates that the rest of the code in the class remains unchanged.
So the fromDeviceTypes method, and other methods that are not related to the size of the UIImageView should still be the same as in the previous implementation. It's just the generateImage method that I've updated to take the imageViewSize into account.
User
can you give me the full code instead?
Assistant
Sure, here's the full updated StackedImageHelper class:
code
import UIKit
class StackedImageHelper {
var iconScale: CGFloat = 0.7
var layers = [UIImage]()
var imageViewSize: CGSize
init(imageViewSize: CGSize) {
self.imageViewSize = imageViewSize
}
func generateImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(size: imageViewSize)
return renderer.image { (context) in
let newWidth = imageViewSize.width
let iconsSize = floor(newWidth * iconScale)
let iconsInset = (newWidth - iconsSize) - 1
if layers.count > 1 {
let secondImage = layers[1]
let secondImageRect = CGRect(x: iconsInset, y: 0, width: iconsSize, height: iconsSize)
secondImage.draw(in: secondImageRect)
}
if layers.count > 0 {
let firstImage = layers[0]
let firstImageRect = CGRect(x: 0, y: iconsInset, width: iconsSize, height: iconsSize)
firstImage.draw(in: firstImageRect)
}
}
}
static func fromDeviceTypes(deviceTypes: [DeviceType], context: UIView) -> StackedImageHelper? {
let firstTwo = Array(deviceTypes.prefix(2))
if firstTwo.count == 1 {
firstTwo.append(firstTwo[0])
}
let imageList = firstTwo.map { UIImage(named: $0.iconOutlinedImageResource) }.compactMap {$0}
let stackedImageHelper = StackedImageHelper(imageViewSize: context.frame.size)
stackedImageHelper.layers = imageList
return stackedImage
User
continue
Assistant
Sure, here's the rest of the updated fromDeviceTypes method:
Now the fromDeviceTypes function takes the size of the context view, which should be the same as the size of the UIImageView where the composite image will be displayed and pass it to the StackedImageHelper constructor. With this change, the size of the generated image will be the same as the size of the UIImageView.