我有一些透明的图像,我从文件系统加载到UIImageView视图中。出于我的目的,我需要将UIImageView中的图像与文件系统上的文件进行比较。因此,我做了如下操作:
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imageFile = [NSString stringWithFormat:@"%@/image.png", directoryPath];
if ([[NSData dataWithContentsOfFile:imageFile] isEqualToData:UIImagePNGRepresentation([imageView image])]) {
NSLog(@"Equal");
} else {
NSString *dataDescription = [[[NSData dataWithContentsOfFile:feltFile] description] substringToIndex:100];
NSString *imageDescription = [[UIImagePNGRepresentation([backgroundImageView image]) description] substringToIndex:100]
NSLog(@"Unequal: %@ %@", dataDescription, imageDescription);
}我知道它们是PNG图像。当我打印时,两个描述都不是空的。但它们是不平等的。
为什么会发生这种情况?
发布于 2011-01-08 04:19:36
您不能指望图像的PNG表示保持逐位不变。有多种方法可以对同一图像进行编码,并且可能还有元数据可以在不同的编码之间进行更改。
相反,您可以逐个像素地比较图像。有关获取UIImage像素数据的信息,请参阅以下内容:How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?
发布于 2011-01-08 04:14:26
UIImagePNGRepresentation()将具有通用类型不可知形式的图像转换为具有标准化设置的PNG。多个PNG文件可以对相同的图像进行编码,当您读入一个PNG文件时,您会丢失相关的元数据,然后在您写出新的PNG文件时会重新创建这些元数据(可能会有所不同)。
你想要达到什么目的?告诉我们,我们可以为您的问题提供更好的解决方案。
https://stackoverflow.com/questions/4629773
复制相似问题