比較影象

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.
}