UIImage is equal to

前端 未结 3 956
南笙
南笙 2021-01-14 11:17

I need to check if a file loaded into an UIImage object file is equal to another image and execute some actions if so. Unfortunately, it\'s not working.

3条回答
  •  我在风中等你
    2021-01-14 12:06

    You can convert your UIImage instances to NSData instances and compare them.

    if let emptyImage = UIImage(named: "empty") {
        let emptyData = UIImagePNGRepresentation(emptyImage)
        let compareImageData = UIImagePNGRepresentation(image1.image)
    
        if let empty = emptyData, compareTo = compareImageData {
            if empty.isEqualToData(compareTo) {
                // Empty image is the same as image1.image
            } else {
                // Empty image is not equal to image1.image
            }
        } else {
            // Creating NSData from Images failed
        }
    }
    

提交回复
热议问题