74

我正在开发一个应用程序。因为我在更改图像imageviews之前使用.SO,所以UIImageview我需要在对象中获取该图像UIimage并与另一个UIImage对象进行比较,以发现两者是否相同。所以请告诉我该怎么做。

4

19 回答 19

135

一种方法是先将它们转换为图像数据,然后进行比较。

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqual:data2];
}
于 2012-07-05T11:01:02.493 回答
29

@Simon 答案的 Swift 实现:

func image(image1: UIImage, isEqualTo image2: UIImage) -> Bool {
    let data1: NSData = UIImagePNGRepresentation(image1)!
    let data2: NSData = UIImagePNGRepresentation(image2)!
    return data1.isEqual(data2)
}

或者根据@nhgrif 的建议扩展 UIImage :

import UIKit

extension UIImage {

    func isEqualToImage(image: UIImage) -> Bool {
        let data1: NSData = UIImagePNGRepresentation(self)!
        let data2: NSData = UIImagePNGRepresentation(image)!
        return data1.isEqual(data2)
    }

}
于 2015-11-26T11:41:41.270 回答
13

更新了 Mark Tickner 对 Swift 4 的解决方案

import UIKit

extension UIImage {

    func isEqualToImage(_ image: UIImage) -> Bool {
        let data1 = self.pngData()
        let data2 = image.pngData()
        return data1 == data2
    }

}

这两个变量可能有点矫枉过正,但它们可能有助于向对此不熟悉的人解释。可以缩短为:

import UIKit

extension UIImage {

    func isEqualToImage(_ image: UIImage) -> Bool {
        return self.pngData() == image.pngData()
    }

}
于 2019-02-25T18:29:28.407 回答
9

当两者都使用时[UIImage imageNamed:],我们可以使用isEqual:,否则我们可以比较数据。

于 2012-11-06T01:22:04.850 回答
7

我的首选(Swift)解决方案

import UIKit

func ==(lhs: UIImage, rhs: UIImage) -> Bool
{
  guard let data1 = UIImagePNGRepresentation(lhs),
            data2 = UIImagePNGRepresentation(rhs)
    else { return false }

  return data1.isEqual(data2)
}
于 2016-03-17T01:02:55.273 回答
7

正确答案取决于“您要进行什么样的比较?”。

  1. 简单的方法就是比较数据。
  2. 如果你想知道图像是否是从一个本地文件创建的——你可以使用 -isEqual: (但有一种危险的方法,因为我不确定如果图像缓存由于某种原因被清除会发生什么)。
  3. 困难的方法是提供每个像素的比较(当然,系统会花更多的时间在上面)。由于法律原因,我无法提供我们公司图书馆的代码:(

但是你可以在这里查看 facebook 的 ios-snapshot-test-case 项目的好例子:link right to the required file。您可以使用性能测试来衡量流程时间。

为了大法官,我将从下面复制代码:

- (BOOL)fb_compareWithImage:(UIImage *)image tolerance:(CGFloat)tolerance
{
  NSAssert(CGSizeEqualToSize(self.size, image.size), @"Images must be same size.");

  CGSize referenceImageSize = CGSizeMake(CGImageGetWidth(self.CGImage), CGImageGetHeight(self.CGImage));
  CGSize imageSize = CGSizeMake(CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage));

  // The images have the equal size, so we could use the smallest amount of bytes because of byte padding
  size_t minBytesPerRow = MIN(CGImageGetBytesPerRow(self.CGImage), CGImageGetBytesPerRow(image.CGImage));
  size_t referenceImageSizeBytes = referenceImageSize.height * minBytesPerRow;
  void *referenceImagePixels = calloc(1, referenceImageSizeBytes);
  void *imagePixels = calloc(1, referenceImageSizeBytes);

  if (!referenceImagePixels || !imagePixels) {
    free(referenceImagePixels);
    free(imagePixels);
    return NO;
  }

  CGContextRef referenceImageContext = CGBitmapContextCreate(referenceImagePixels,
                                                             referenceImageSize.width,
                                                             referenceImageSize.height,
                                                             CGImageGetBitsPerComponent(self.CGImage),
                                                             minBytesPerRow,
                                                             CGImageGetColorSpace(self.CGImage),
                                                             (CGBitmapInfo)kCGImageAlphaPremultipliedLast
                                                             );
  CGContextRef imageContext = CGBitmapContextCreate(imagePixels,
                                                    imageSize.width,
                                                    imageSize.height,
                                                    CGImageGetBitsPerComponent(image.CGImage),
                                                    minBytesPerRow,
                                                    CGImageGetColorSpace(image.CGImage),
                                                    (CGBitmapInfo)kCGImageAlphaPremultipliedLast
                                                    );

  if (!referenceImageContext || !imageContext) {
    CGContextRelease(referenceImageContext);
    CGContextRelease(imageContext);
    free(referenceImagePixels);
    free(imagePixels);
    return NO;
  }

  CGContextDrawImage(referenceImageContext, CGRectMake(0, 0, referenceImageSize.width, referenceImageSize.height), self.CGImage);
  CGContextDrawImage(imageContext, CGRectMake(0, 0, imageSize.width, imageSize.height), image.CGImage);

  CGContextRelease(referenceImageContext);
  CGContextRelease(imageContext);

  BOOL imageEqual = YES;

  // Do a fast compare if we can
  if (tolerance == 0) {
    imageEqual = (memcmp(referenceImagePixels, imagePixels, referenceImageSizeBytes) == 0);
  } else {
    // Go through each pixel in turn and see if it is different
    const NSInteger pixelCount = referenceImageSize.width * referenceImageSize.height;

    FBComparePixel *p1 = referenceImagePixels;
    FBComparePixel *p2 = imagePixels;

    NSInteger numDiffPixels = 0;
    for (int n = 0; n < pixelCount; ++n) {
      // If this pixel is different, increment the pixel diff count and see
      // if we have hit our limit.
      if (p1->raw != p2->raw) {
        numDiffPixels ++;

        CGFloat percent = (CGFloat)numDiffPixels / pixelCount;
        if (percent > tolerance) {
          imageEqual = NO;
          break;
        }
      }

      p1++;
      p2++;
    }
  }

  free(referenceImagePixels);
  free(imagePixels);

  return imageEqual;
}
于 2016-04-20T08:25:29.353 回答
6

将图像转换为 JPG / PNG,或依赖可访问性标识符要么是一项昂贵的操作,要么是脆弱的且容易失败。

在这里,我遵循 Apple 在以下链接中提供的建议:

isEqual( :) 方法是确定两个图像是否包含相同图像数据的唯一可靠方法。您创建的图像对象可能彼此不同,即使您使用相同的缓存图像数据对其进行初始化也是如此。确定它们是否相等的唯一方法是使用 isEqual( :) 方法,该方法比较实际的图像数据。清单 1 说明了比较图像的正确和错误方法。

为了简化事情,我创建了以下扩展来进行比较,这样我就可以避免转换第一张图像的问题:

import UIKit

extension UIImage {
    func isEqual(to image: UIImage) -> Bool {
        return isEqual(image)
    }
}

有了这个,我现在可以设置一个示例来在一对图像上进行比较:

let imageA = UIImage(named: "a")!
let imageB = UIImage(named: "b")!
let imageC = UIImage(named: "a")!

print(imageA.isEqual(to: imageA)) // true
print(imageA.isEqual(to: imageC)) // true
print(imageA.isEqual(to: imageB)) // false
于 2017-06-07T14:53:09.233 回答
5

我对 Mark 的回答做了一些更改,并使用了 Data 和 elementsEqual 而不是 NSData 和 isEqual。

extension UIImage {
    func isEqual(to image: UIImage) -> Bool {
        guard let data1: Data = UIImagePNGRepresentation(self),
            let data2: Data = UIImagePNGRepresentation(image) else {
                return false
        }
        return data1.elementsEqual(data2)
    }
}
于 2018-05-30T20:58:07.820 回答
5

Swift 4.x 版本 Facebook 的比较算法:

/// Value in range 0...100 %
typealias Percentage = Float

// See: https://github.com/facebookarchive/ios-snapshot-test-case/blob/master/FBSnapshotTestCase/Categories/UIImage%2BCompare.m
private func compare(tolerance: Percentage, expected: Data, observed: Data) throws -> Bool {
    guard let expectedUIImage = UIImage(data: expected), let observedUIImage = UIImage(data: observed) else {
        throw Error.unableToGetUIImageFromData
    }
    guard let expectedCGImage = expectedUIImage.cgImage, let observedCGImage = observedUIImage.cgImage else {
        throw Error.unableToGetCGImageFromData
    }
    guard let expectedColorSpace = expectedCGImage.colorSpace, let observedColorSpace = observedCGImage.colorSpace else {
        throw Error.unableToGetColorSpaceFromCGImage
    }
    if expectedCGImage.width != observedCGImage.width || expectedCGImage.height != observedCGImage.height {
        throw Error.imagesHasDifferentSizes
    }
    let imageSize = CGSize(width: expectedCGImage.width, height: expectedCGImage.height)
    let numberOfPixels = Int(imageSize.width * imageSize.height)

    // Checking that our `UInt32` buffer has same number of bytes as image has.
    let bytesPerRow = min(expectedCGImage.bytesPerRow, observedCGImage.bytesPerRow)
    assert(MemoryLayout<UInt32>.stride == bytesPerRow / Int(imageSize.width))

    let expectedPixels = UnsafeMutablePointer<UInt32>.allocate(capacity: numberOfPixels)
    let observedPixels = UnsafeMutablePointer<UInt32>.allocate(capacity: numberOfPixels)

    let expectedPixelsRaw = UnsafeMutableRawPointer(expectedPixels)
    let observedPixelsRaw = UnsafeMutableRawPointer(observedPixels)

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    guard let expectedContext = CGContext(data: expectedPixelsRaw, width: Int(imageSize.width), height: Int(imageSize.height),
                                          bitsPerComponent: expectedCGImage.bitsPerComponent, bytesPerRow: bytesPerRow,
                                          space: expectedColorSpace, bitmapInfo: bitmapInfo.rawValue) else {
        expectedPixels.deallocate()
        observedPixels.deallocate()
        throw Error.unableToInitializeContext
    }
    guard let observedContext = CGContext(data: observedPixelsRaw, width: Int(imageSize.width), height: Int(imageSize.height),
                                          bitsPerComponent: observedCGImage.bitsPerComponent, bytesPerRow: bytesPerRow,
                                          space: observedColorSpace, bitmapInfo: bitmapInfo.rawValue) else {
        expectedPixels.deallocate()
        observedPixels.deallocate()
        throw Error.unableToInitializeContext
    }

    expectedContext.draw(expectedCGImage, in: CGRect(origin: .zero, size: imageSize))
    observedContext.draw(observedCGImage, in: CGRect(origin: .zero, size: imageSize))

    let expectedBuffer = UnsafeBufferPointer(start: expectedPixels, count: numberOfPixels)
    let observedBuffer = UnsafeBufferPointer(start: observedPixels, count: numberOfPixels)

    var isEqual = true
    if tolerance == 0 {
        isEqual = expectedBuffer.elementsEqual(observedBuffer)
    } else {
        // Go through each pixel in turn and see if it is different
        var numDiffPixels = 0
        for pixel in 0 ..< numberOfPixels where expectedBuffer[pixel] != observedBuffer[pixel] {
            // If this pixel is different, increment the pixel diff count and see if we have hit our limit.
            numDiffPixels += 1
            let percentage = 100 * Float(numDiffPixels) / Float(numberOfPixels)
            if percentage > tolerance {
                isEqual = false
                break
            }
        }
    }

    expectedPixels.deallocate()
    observedPixels.deallocate()

    return isEqual
}
于 2018-12-28T12:00:49.363 回答
4

斯威夫特 3

有两种方法。像:-

1) 使用 isEqual() 函数。

 self.image?.isEqual(UIImage(named: "add-image"))

2)使用accessibilityIdentifier

将accessibilityIdentifier设置为图像名称

myImageView.image?.accessibilityIdentifier = "add-image"

然后使用以下代码。

extension UIImageView
{
func getFileName() -> String? {
// First set accessibilityIdentifier of image before calling.
let imgName = self.image?.accessibilityIdentifier
return imgName
}
}

最后,识别方法的调用方式

myImageView.getFileName()
于 2017-04-13T10:50:31.453 回答
3

斯威夫特 5.x

你可以使用

let image: UIImage!
let anotherImage: UIImage!

image == anotherImage
于 2019-10-17T09:22:20.457 回答
2

最初比较图像大小

对于较便宜的方法,首先比较图像大小。即使图像内部有微小的变化,大小也会有所不同。

NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);

if(data1.length == data2.length) {
    // if required, compare the data to confirm it
    if(data1 isEqual:data2) {
        // images are exactly same
    } else {
        // even though size is same images are different
    }
} else {
    // images are different.
}

在比较来自相同来源(相同尺寸、格式等)的图像时成功进行了测试。

于 2015-07-23T13:32:56.653 回答
1

这种方法效果很好:

func isEqualImages(image1: UIImage, image2: UIImage) -> Bool {
    let data1: Data? = UIImagePNGRepresentation(image1)
    let data2: Data? = UIImagePNGRepresentation(image2)
    return data1 == data2
}
于 2018-03-03T01:03:42.343 回答
1

速记和 Swifty 解决方案

extension UIImage: Equatable {
    static func ==(lhs: UIImage, rhs: UIImage) -> Bool {
        return lhs.pngData() == rhs.pngData()
    }
}
于 2019-12-02T14:31:55.123 回答
0

我不确定比较 UIImage 数据,因为它会非常昂贵。你可以做的是继承 Uimage 并添加你自己的标签属性,然后在更改图像之前比较标签。分配

于 2015-07-03T21:38:27.523 回答
0

我需要检测视频馈送帧中的差异并阈值该差异,以便决定在我的代码中执行某些操作。您可以使用类似的像素比较来查看 UIImage 的 pngData。

在这里查看我的答案

于 2020-01-12T22:17:38.557 回答
-1

如果您知道一个图像名称,它将对您有所帮助....

CGImageRef cgImage = [imageView.image CGImage];
if (cgImage == [UIImage imageNamed:@"imagename.png"].CGImage) {
   // True Statement
}
于 2017-05-03T10:38:02.470 回答
-1

我们需要在 Objective-C 中使用 isEqualToData 方法。苹果文件指出

Two data objects are equal if they hold the same number of bytes, and if the bytes at the same position in the objects are the same.

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqualToData:data2];
}
于 2020-08-10T17:30:50.130 回答
-2

好吧,您还可以使用“标记”属性来识别程序后期的对象。只需设置整数值就可以了

于 2012-09-05T13:20:07.063 回答