gpt4 book ai didi

ios - 将一张图像分成多个部分后,如何在 Swift 中使用 UIImage 数组?

转载 作者:行者123 更新时间:2023-11-30 12:12:29 25 4
gpt4 key购买 nike

我正在关注this dicussion并得到了一组图像,这些图像是原始图像的划分部分。如果我打印它,它看起来像这样:

[<UIImage: 0x61000008ea60>, {309, 212}, <UIImage: 0x61000008ec90>, {309, 212}, <UIImage: 0x61000008ebf0>, {309, 213}, <UIImage: 0x61000008ec40>, {309, 213}]

我如何使用这个数组的元素?在常规情况下,我将拥有图像的名称并可以使用,例如 UIImage(named: "")。该数组没有向我显示任何名称。你有什么想法吗?

也许我的错误在这里:

 func setImage(){


testImage = UIImageView(frame: CGRect(x: 0, y: 0, width: size/1.5, height: size/1.5))
testImage.center = CGPoint(x: view.frame.width/2, y: view.frame.height/2)


slice(image: UIImage(named:"leopard_PNG14834")!, into: 8)

testImage.image = images[2]


view.addSubview(testImage)


}

这是函数代码

func slice(image: UIImage, into howMany: Int) -> [UIImage] {
let width: CGFloat
let height: CGFloat

switch image.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
width = image.size.height
height = image.size.width
default:
width = image.size.width
height = image.size.height
}

let tileWidth = Int(width / CGFloat(howMany))
let tileHeight = Int(height / CGFloat(howMany))

let scale = Int(image.scale)


let cgImage = image.cgImage!

var adjustedHeight = tileHeight

var y = 0
for row in 0 ..< howMany {
if row == (howMany - 1) {
adjustedHeight = Int(height) - y
}
var adjustedWidth = tileWidth
var x = 0
for column in 0 ..< howMany {
if column == (howMany - 1) {
adjustedWidth = Int(width) - x
}
let origin = CGPoint(x: x * scale, y: y * scale)
let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
let tileCgImage = cgImage.cropping(to: CGRect(origin: origin, size: size))!
images.append(UIImage(cgImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation))
x += tileWidth
}
y += tileHeight
}
return images
}

最佳答案

如果您很难将图像添加到 ViewController,这是您常用的方法,假设输入是图像数组:

let image = images[0]
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)

编辑

如果您弄乱了 ImageView 的框架,我建议您在使用上面的图像初始化 UIImageView 后执行此操作。要测试发生了什么,也许只需添加一个带有所需图像的 ImageView 即可,而不会弄乱框架。然后检查用于设置框架的变量。

关于ios - 将一张图像分成多个部分后,如何在 Swift 中使用 UIImage 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45882314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com