Skip to content

Instantly share code, notes, and snippets.

@hpique
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpique/dedb719817ac8c50afea to your computer and use it in GitHub Desktop.
Save hpique/dedb719817ac8c50afea to your computer and use it in GitHub Desktop.
UIImage category to compare two images pixel by pixel.
//
// 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
//
// 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
@hpique
Copy link
Author

hpique commented May 18, 2014

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.

@rsaunders100
Copy link

Is there a difference between [image hp_normalizedData] and UIImagePNGRepresentation(image)?

@hpique
Copy link
Author

hpique commented Aug 20, 2014

@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