views:

126

answers:

2

How can I compare an image to another one?

Thanks!

A: 

Your best bet is to get a CGImage from UIImage and than use CGImageGetDecode to get the raw data and compare that.

Steam Trout
CGImageGetDecode doesn't get what you think it does.
quixoto
So don't use the above? Then what should I do?
A: 

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:

http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-CJBHBFFE

The difference for you is that you're drawing an existing image into the context. Use CGContextDrawImage for this.

quixoto
Whoaaaa...Thats kinda confusing im not looking for a free ride (i actually want to learn from this) but could you post some code. Im not really framillar with any CG methods..Or at least try to explain it beter for a noob.
I've added a link to good info. If it's still a little over your head, check out tutorials and Apple's docs on the basics of Core Graphics. It's too big a subject to answer as a question here on SO, and you need to understand that if you're doing any substantial image work on iOS.
quixoto
Erhm That was confusing... Someone said you could just convert it to binary image or just grayscale and compare that. Could I do that?
Yes, and that's exactly what my answer (and the Apple docs) tell you how to do. :) Sorry, but given two UIImages, the only way to compare the pixels directly is to do some nitty-gritty work with Core Graphics contexts. Maybe someone else here will be kind enough to provide complete code for copy and paste.
quixoto
meh. The only hard part is I dont know how to find some of the arguments required. (everything except width and height.) I dont mind doing some hard coding.
Cool, then either update this question, or post a new one asking about the specific arguments you don't understand and how to know what to use for them.
quixoto
Im going to put this project on hold, since its more complex than I thought and I have somehting I want to Finish first. I'll ask when Im ready thanks thought!!