【问题标题】:How to compare UIImages in arrays?如何比较数组中的 UIImages?
【发布时间】:2015-11-19 13:34:02
【问题描述】:

我正在制作一个应用程序,能够比较一个数组/集合中的值会很好。

假设我有这样的常量和数组:

let blueImage = UIImage(named: "blue")
let redImage = UIImage(named: "red")

button.setImage(blueImage, forState: .Normal)
button2.setImage(redImage, forState: .Normal)
button3.setImage(blueImage, forState: .Normal)

var imageArray:Array<UIImage> = [button.currentImage, button2.currentImage, button3.currentImage]

然后是否可以检查/比较我的数组中的值并将红色图像替换为蓝色图像。

更具体地说,有一种方法可以检查数组中 2/3 的图像是否包含特定图像(blueImage),然后将最后一个值(redImage)替换为(blueImage)所以都有相同的图片。

【问题讨论】:

    标签: ios arrays swift uiimage


    【解决方案1】:

    我想您可以使用以下内容过滤数组:

    let filteredArray = filter(imageArray) { $0 == blueImage }
    

    然后进行计数。

    你也可以遍历你的数组:

    let countBlue = 0
    
    for i in 0..<imageArray.count {
    
        if imageArray[i] == blueImage {
            countBlue ++
        }
    }
    

    替换元素:

    imageArray[2] = blueImage
    

    【讨论】:

    • @Polis 我不确定{ $0 == blueImage } 是做什么的,你能解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多