iOS在UIImages的逐像素比较中检索不同的像素

我试图对两个UIImages进行逐像素比较,我需要检索不同的像素。 使用来自UIImage的Generate哈希,我找到了一种为UIImage生成哈希的方法。 有没有办法比较两个哈希并检索不同的像素?

如果您想要实际检索差异,哈希不能帮助您。 您可以使用哈希来检测可能存在的差异,但要获得实际差异,您必须使用其他技术。

例如,要创建一个由两个图像之间的差异组成的UIImage ,请参阅此接受的答案 ,其中Cory Kilgor说明了使用CGContextSetBlendMode并使用kCGBlendModeDifference的混合模式:

 + (UIImage *) differenceOfImage:(UIImage *)top withImage:(UIImage *)bottom { CGImageRef topRef = [top CGImage]; CGImageRef bottomRef = [bottom CGImage]; // Dimensions CGRect bottomFrame = CGRectMake(0, 0, CGImageGetWidth(bottomRef), CGImageGetHeight(bottomRef)); CGRect topFrame = CGRectMake(0, 0, CGImageGetWidth(topRef), CGImageGetHeight(topRef)); CGRect renderFrame = CGRectIntegral(CGRectUnion(bottomFrame, topFrame)); // Create context CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if(colorSpace == NULL) { printf("Error allocating color space.\n"); return NULL; } CGContextRef context = CGBitmapContextCreate(NULL, renderFrame.size.width, renderFrame.size.height, 8, renderFrame.size.width * 4, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); if(context == NULL) { printf("Context not created!\n"); return NULL; } // Draw images CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, CGRectOffset(bottomFrame, -renderFrame.origin.x, -renderFrame.origin.y), bottomRef); CGContextSetBlendMode(context, kCGBlendModeDifference); CGContextDrawImage(context, CGRectOffset(topFrame, -renderFrame.origin.x, -renderFrame.origin.y), topRef); // Create image from context CGImageRef imageRef = CGBitmapContextCreateImage(context); UIImage * image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGContextRelease(context); return image; }