简体   繁体   English

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

[英]How to use an UIImage array in Swift after dividing one image into multiple parts?

I am following this dicussion and got an array of images which are divided parts of the original image. 我按照这个讨论 ,得到了一系列图像,这些图像是原始图像的各个部分。 If I print it, it looks like this: 如果我打印它,它看起来像这样:

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

How could I use elements of this array? 如何使用此数组的元素? In the regular case I would have the name of the image and could use, for example, UIImage(named: "") . 在正常情况下,我将具有图像的名称,并可以使用例如UIImage(named: "") This array doesn't show me any names. 该数组不显示任何名称。 Do you have any idea? 你有什么主意吗?

perhaps my mistake is here: 也许我的错误在这里:

 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)


}

Here is the function code 这是功能代码

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
}

You already get an array with objects of type UIImage . 您已经获得了带有UIImage类型对象的数组。

let images = slice(image: someOriginalImage, into: 4)
let firstImage = images[0] // firstImage is a UIImage

{309, 212} is a size of the image. {309, 212}是图像的尺寸。

If you're having a hard time adding your images to your ViewController, this is the common approach you would use to do so, assuming an input of an array of images: 如果您在将图像添加到ViewController时遇到困难,这是您通常使用的方法,假设输入了图像数组:

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

EDIT 编辑

If you're messing with the frame of the image view, I would suggest doing it after you've initialized the UIImageView with an image like above. 如果您弄乱了图像视图的框架,建议您使用上述图像初始化UIImageView之后再进行操作。 To test what's going on, perhaps just add an ImageView with your desired image without messing with the frame. 要测试正在发生的事情,也许只需添加一个带有所需图像的ImageView而不弄乱框架。 Then check your variables that you are using to set the frame. 然后检查用于设置框架的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM