iphone - UIImagePNGRepresentation() 返回不同的字节

标签 iphone uiimage png nsdata uiimagepngrepresentation

我有一些透明图像,正在从文件系统加载到 UIImageView View 中。出于我的目的,我需要将 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 图像。当我打印它时,这两个描述都不为 NULL。但他们是不平等的。

为什么会发生这种情况?

最佳答案

您不能指望图像的 PNG 表示形式保持不变。有多种方法可以对同一图像进行编码,并且可能还存在可以在编码之间发生变化的元数据。

相反,您可以逐像素比较图像。请参阅以下内容以获取 UIImage 的像素数据:How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

关于iphone - UIImagePNGRepresentation() 返回不同的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629773/

相关文章:

c++ - lodePNG 内存分配错误

iphone - 如何在 Monotouch 中对 UIImageView 进行运动模糊效果?

iphone - 智能手机内存配置

ios - 无法将 UIImage 存储在 NSMutableDictionary 中

ios - 在后台任务中添加到 CALayer 时不显示 UIImage

ios - 如何通过 Storyboard设置 UISegmentedControl 背景图像?

image-processing - 用于创建 PNG 8 位透明图像的软件

png - Aero 窗口上的 Autohotkey 透明 PNG

ios - 如何添加当前时间(HH :MM:SS) to NSDate?

iphone - 如何在 AVPlayer 中设置像 AVAudioPlayer 这样的 pan 属性