因此,Apple的文档说CIImage
符合Equatable
。我认为这意味着以下单元测试将通过。但事实并非如此。我对原因感兴趣。
func test_CIImageEqualityShouldWork() {
let bundle = NSBundle(forClass: PrototypeTests.self)
guard let path = bundle.pathForResource("testImage", ofType: "png") else { return }
guard let image = UIImage(contentsOfFile: path) else { return }
let thingy1 = CIImage(image: image)
let thingy2 = CIImage(image: image)
XCTAssert(thingy1 == thingy2)
}
图像存在,guard
语句都通过,但断言失败,它们不相等。
出于兴趣,我尝试了两次创建UIImage
并进行比较。那也失败了。
答案 0 :(得分:2)
所有NSObject
子类都符合Equatable
和==
函数
在对象上调用isEqual:
方法。
isEqual:
方法
NSObject
只是比较对象指针,即o1 == o2
如果o1
和o2
引用相同的对象实例,则成立。
参见例如 Interacting with Objective-C APIs:
Swift提供了==和===运算符的默认实现 对于派生自的对象采用Equatable协议 NSObject类。 ==运算符的默认实现调用 isEqual:方法,以及===的默认实现 运算符检查指针相等。你不应该重写相等 或从Objective-C导入的类型的标识运算符。
isEqual的基本实现:由NSObject类提供 等同于通过指针相等的身份检查。
许多NSObject
个子类覆盖了isEqual:
方法(例如NSString
,
NSArray
,NSDate
,...)但不是CIImage
:
let thingy1 = CIImage(image: image)
let thingy2 = CIImage(image: image)
创建两个不同的CIImage
个实例,这些实例比较为“不相等”。
答案 1 :(得分:0)
FlexMonkey的ImageCompareDemo是一个不完整的端口,来自Facebook的C {+ {3}}到Swift。它省略了每像素像素比较的最后部分。我的是Swift 4,这是整个功能:
static func compareWithImage(reference:CGImage, target:CGImage, tolerance:CGFloat) -> Bool {
guard reference.width == target.width && reference.height == target.height else { return false }
let referenceImageSize = CGSize(width:CGFloat(reference.width), height:CGFloat(reference.height))
let targetImageSize = CGSize(width:CGFloat(target.width), height:CGFloat(target.height))
let minBytesPerRow = min(reference.bytesPerRow, target.bytesPerRow)
let referenceImageSizeBytes = Int(referenceImageSize.height) * minBytesPerRow
let referenceImagePixels = calloc(1, referenceImageSizeBytes)
let targetImagePixels = calloc(1, referenceImageSizeBytes)
let referenceImageCtx = CGContext(data: referenceImagePixels,
width: Int(referenceImageSize.width),
height: Int(referenceImageSize.height),
bitsPerComponent: reference.bitsPerComponent,
bytesPerRow: minBytesPerRow,
space: reference.colorSpace!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
let targetImageCtx = CGContext(data: targetImagePixels,
width: Int(targetImageSize.width),
height: Int(targetImageSize.height),
bitsPerComponent: target.bitsPerComponent,
bytesPerRow: minBytesPerRow,
space: target.colorSpace!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
guard let referenceImageContext = referenceImageCtx, let targetImageContext = targetImageCtx else {
return false
}
referenceImageContext.draw(reference, in:CGRect(x:0, y:0, width:referenceImageSize.width, height:referenceImageSize.height))
targetImageContext.draw(target, in:CGRect(x:0, y:0, width:targetImageSize.width, height:targetImageSize.height))
var imageEqual = true
if(tolerance == 0) {
imageEqual = (memcmp(referenceImagePixels, targetImagePixels, referenceImageSizeBytes) == 0)
} else {
let pixelCount = Int(referenceImageSize.width * referenceImageSize.height)
let p1 = convertUMRPtoUInt32Array(pointer:referenceImagePixels!, length:referenceImageSizeBytes)
let p2 = convertUMRPtoUInt32Array(pointer:targetImagePixels!, length:referenceImageSizeBytes)
var percent:CGFloat = 0
var numDiffPixels = 0
for n in 0..<pixelCount {
if(p1[n] != p2[n]) {
numDiffPixels += 1
percent = CGFloat(numDiffPixels) / CGFloat(pixelCount)
if (percent > tolerance) {
imageEqual = false;
break;
}
}
}
//print(percent)
}
referenceImagePixels?.deallocate(bytes:referenceImageSizeBytes, alignedTo:1)
targetImagePixels?.deallocate(bytes: referenceImageSizeBytes, alignedTo: 1)
return imageEqual
}