Last active
January 29, 2016 16:55
-
-
Save FlexMonkey/c4c77c9562cf252a5660 to your computer and use it in GitHub Desktop.
Compares two images to see if they have identical content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is maybe a nice solution, but it's not the best solution.
See this project for a comparison: https://github.com/FlexMonkey/ImageCompareDemo