比较图像
isEqual:
方法是确定两个图像是否包含相同图像数据的唯一可靠方法。你创建的图像对象可能彼此不同,即使使用相同的缓存图像数据初始化它们也是如此。确定其相等性的唯一方法是使用isEqual:
方法,该方法比较实际图像数据。清单 1 说明了比较图像的正确和错误方法。来源: Apple 文档
迅速
// Load the same image twice.
let image1 = UIImage(named: "MyImage")
let image2 = UIImage(named: "MyImage")
// The image objects may be different, but the contents are still equal
if let image1 = image1, image1.isEqual(image2) {
// Correct. This technique compares the image data correctly.
}
if image1 == image2 {
// Incorrect! Direct object comparisons may not work.
}
Objective-C
// Load the same image twice.
UIImage* image1 = [UIImage imageNamed:@"MyImage"];
UIImage* image2 = [UIImage imageNamed:@"MyImage"];
// The image objects may be different, but the contents are still equal
if ([image1 isEqual:image2]) {
// Correct. This technique compares the image data correctly.
}
if (image1 == image2) {
// Incorrect! Direct object comparisons may not work.
}