How to compare two UIImage objects
One way is to convert them to image data first, and then compare that.
- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqual:data2];
}
UIImage is equal to
You can convert your UIImage
instances to NSData
instances and compare them.
if let emptyImage = UIImage(named: "empty") {
let emptyData = UIImagePNGRepresentation(emptyImage)
let compareImageData = UIImagePNGRepresentation(image1.image)
if let empty = emptyData, compareTo = compareImageData {
if empty.isEqualToData(compareTo) {
// Empty image is the same as image1.image
} else {
// Empty image is not equal to image1.image
}
} else {
// Creating NSData from Images failed
}
}
Comparing UIImage
If you have two UIImages, you should get their CGImageRef
quartz representations from those objects. Then create two new bitmap contexts backed by a memory buffer that you create and pass in, one for each of the images. Then use CGContextDrawImage
to draw the images into the bitmap contexts. Now the bytes of the images are in the buffers. You can then loop through manually or memcmp
to check for differences.
Apple's own detailed explanation and sample code around creating bitmap contexts and drawing into them is here:
https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/
dq_context/dq_context.html
The difference for you is that you're drawing an existing image into the context. Use CGContextDrawImage
for this.
How to compare two UIImage in iOS 8
Please compare NSData object instead of comparing UIImage Object.
-(BOOL)firstimage:(UIImage *)image1 isEqualTo:(UIImage *)image2 {
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqualToData:data2];
}
Compare 2 UIImage objects
If you need to compare images on pixel equality, not pointer, you can do this:
You can create some kind of hash for every image when you create it. For example, the sum of
all pixel values (maybe, modulo some huge number; maybe, powered by pixel position).
Then, store that value in NSDictionary[image] = hashValue
. Then, when you compare images, first compare their sizes, as you mentioned, and then, if they are equal, compare their hashes from dictionary.
If they are equal, then images are most possibly equal, but you have to check it manually to be 100% sure.
Manual checking may take some time, so you can decrease the probability of collision (when different images have same hashes) by inventing your own statistics, like MORE HASHES, different modulo, hashes of hashes, value of left-topmost pixel (kidding, but who knows...) and so on.
It is obvious, that the more statistics you have, the less collisions you'll got.
The field of experiments :)
Otherwise, just compare their pointer addresses like this
if(image1 == image2) {
...
}
Checking for comparing UIImages
I think you should check the size
of the image first. If size
and scale
of both images are equal, check the pixel data directly for equality, and not the images PNG representation. this will be much faster. (The link shows you how to get the pixel data. To compare it, use memcmp
.)
From that post (slightly modified):
NSData *rawDataFromUIImage(UIImage *image)
{
assert(image);
// Get the image into the data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int byteSize = height * width * 4;
unsigned char *rawData = (unsigned char*) malloc(byteSize);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
return [NSData dataWithBytes:rawData length:byteSize];
}
About why this is faster: UIImagePNGRepresentation
(1) fetches the raw binary data and then (2) converts it to PNG format. Skipping the second step can only improve performance, because it is much more work than just doing step 1. And memcmp
is faster than everything else in this example.
How to compare UIImages in arrays?
I guess you could filter the array with something along these lines:
let filteredArray = filter(imageArray) { $0 == blueImage }
and then run a count.
You could also iterate over your array:
let countBlue = 0
for i in 0..
if imageArray[i] == blueImage {
countBlue ++
}
}
To replace an element:
imageArray[2] = blueImage
Swift Image Name Compare
You cannot directly compare two images are same. You can get the images as NSDATA and then you can compare two NSDATA values are equal.
Related Topics
iOS App Error - Can't Add Self as Subview
Static Table View Outside Uitableviewcontroller
iOS Memory Allocation - How Much Memory Can Be Used in an Application
iOS 7 App Icons, Launch Images and Naming Convention While Keeping iOS 6 Icons
Xcode 8 - Ib Designables - Failed to Render and Update Auto Layout Status, the Agent Crashed
Swift Programmatically Navigate to Another View Controller/Scene
iOS - Uiimageview - How to Handle Uiimage Image Orientation
How to Detect the Touch Event of an Uiimageview
How to Get Audio Volume Level, and Volume Changed Notifications on iOS
Using Iskindofclass with Swift
Google Sign-In Crashes on iOS 9 Attempting to Call Canopenurl
How to Change Device Volume on iOS - Not Music Volume
iOS 5 Twitter Framework: Tweeting Without User Input and Confirmation (Modal View Controller)
Why Is Main Window of Type Double Optional
Store [String] in Nsuserdefaults
Uigraphicsgetimagefromcurrentimagecontext Memory Leak with Previews
My Prerelease App Has Been "Processing" for Over a Week in Itunes Connect, What Gives