Last active
August 29, 2015 14:01
-
-
Save hpique/dedb719817ac8c50afea to your computer and use it in GitHub Desktop.
UIImage category to compare two images pixel by pixel.
This file contains 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
// | |
// UIImage+HPIsEqualToImage.h | |
// | |
// Created by Hermes Pique on 20/02/14. | |
// Copyright (c) 2014 Hermes Pique. All rights reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImage (HPIsEqualToImage) | |
- (BOOL)hp_isEqualToImage:(UIImage*)image; | |
- (NSData*)hp_normalizedData; | |
@end |
This file contains 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
// | |
// UIImage+HPIsEqualToImage.m | |
// | |
// Created by Hermes Pique on 20/02/14. | |
// Copyright (c) 2014 Hermes Pique. All rights reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
#import "UIImage+HPIsEqualToImage.h" | |
@implementation UIImage (HPIsEqualToImage) | |
- (BOOL)hp_isEqualToImage:(UIImage*)image | |
{ | |
NSData *data = [image hp_normalizedData]; | |
NSData *originalData = [self hp_normalizedData]; | |
return [originalData isEqualToData:data]; | |
} | |
- (NSData*)hp_normalizedData | |
{ | |
const CGSize pixelSize = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale); | |
UIGraphicsBeginImageContext(pixelSize); | |
[self drawInRect:CGRectMake(0, 0, pixelSize.width, pixelSize.height)]; | |
UIImage *drawnImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return UIImagePNGRepresentation(drawnImage); | |
} | |
@end | |
Is there a difference between [image hp_normalizedData]
and UIImagePNGRepresentation(image)
?
@rsaunders Yes. There is no guarantee that two images with the same pixel values will have the same PNG data representation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I use in my unit tests to compare images. It's not very efficient, so only use it in production code if performance is not a concern.