UIImage 等于 [英] UIImage is equal to
本文介绍了UIImage 等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查加载到 UIImage
对象文件中的文件是否等于另一个图像,如果是,则执行一些操作.不幸的是,它不起作用.
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.
emptyImage = UIImage(named: imageName)
if(image1.image != emptyImage) {
// do something
} else {
// do something
}
上面的代码总是进入if
分支.
The above code always enters the if
branch.
推荐答案
您可以将 UIImage
实例转换为 NSData
实例并进行比较.
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
}
}
这篇关于UIImage 等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文