Skip to content

Instantly share code, notes, and snippets.

@FlexMonkey
Last active January 29, 2016 16:55
Show Gist options
  • Save FlexMonkey/c4c77c9562cf252a5660 to your computer and use it in GitHub Desktop.
Save FlexMonkey/c4c77c9562cf252a5660 to your computer and use it in GitHub Desktop.
Compares two images to see if they have identical content
func UIImageEqualToImage(image1: UIImage, _ image2: UIImage, ciContext: CIContext? = nil) -> Bool
{
guard let
ciImage1 = CIImage(image: image1),
ciImage2 = CIImage(image: image2) where image1.size == image2.size else
{
return false
}
let ctx = ciContext ?? CIContext()
let difference = ciImage1.imageByApplyingFilter("CIDifferenceBlendMode",
withInputParameters: [kCIInputBackgroundImageKey: ciImage2])
.imageByApplyingFilter("CIAreaMaximum",
withInputParameters: [kCIInputExtentKey: CIVector(CGRect: ciImage1.extent)])
let totalBytes = 4
let bitmap = calloc(totalBytes, sizeof(UInt8))
ctx.render(difference,
toBitmap: bitmap,
rowBytes: totalBytes,
bounds: difference.extent,
format: kCIFormatRGBA8,
colorSpace: nil)
let rgba = UnsafeBufferPointer<UInt8>(
start: UnsafePointer<UInt8>(bitmap),
count: totalBytes)
return rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0
}
@FlexMonkey
Copy link
Author

This is maybe a nice solution, but it's not the best solution.

See this project for a comparison: https://github.com/FlexMonkey/ImageCompareDemo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment