比较数组的两个对象时遇到问题

时间:2012-04-20 09:17:03

标签: objective-c arrays comparison compare elements

这个问题可能看似重复,但它有一些特定的时刻,可能与此类其他问题不同......

所以......我有两张照片。两者都被捕获在同一个屏幕上。在我的代码中,将两个(screenCaptureFirst和screenCaptureSecond)大图像裁剪成小的裁剪图像(每个裁剪图像具有32x32维度)。然后我将它们分成两个数组。现在我必须比较两个数组的每个元素。     - (void) differenceDetector{

int index=0;

for (int currentGridY=0; currentGridY<newCapturedImage.size.height; currentGridY+=gridSize) {
    for (int currentGridX=0; currentGridX<newCapturedImage.size.width; currentGridX+=gridSize) {

        CGRect rect=CGRectMake(currentGridX, currentGridY, gridSize, gridSize);
        UIImage *croppedNewImage=[self croppedImage:rect anImage:newCapturedImage];
        [arrayOfNewImageGrids addObject:croppedNewImage];
        UIImage *croppedOldImage=[self croppedImage:rect anImage:oldCapturedImage];
        [arrayOfOldImageGrids addObject:croppedOldImage];

        if ([[arrayOfNewImageGrids objectAtIndex:index]isEqual:[arrayOfOldImageGrids objectAtIndex:index]]) {
            NSLog(@"Index=%d",index);
        }
        NSLog(@"newGridArray=%@",[arrayOfNewImageGrids objectAtIndex:index]);
        NSLog(@"oldGridArray=%@",[arrayOfOldImageGrids objectAtIndex:index]);
        index++;
    }
}

int index=0; for (int currentGridY=0; currentGridY<newCapturedImage.size.height; currentGridY+=gridSize) { for (int currentGridX=0; currentGridX<newCapturedImage.size.width; currentGridX+=gridSize) { CGRect rect=CGRectMake(currentGridX, currentGridY, gridSize, gridSize); UIImage *croppedNewImage=[self croppedImage:rect anImage:newCapturedImage]; [arrayOfNewImageGrids addObject:croppedNewImage]; UIImage *croppedOldImage=[self croppedImage:rect anImage:oldCapturedImage]; [arrayOfOldImageGrids addObject:croppedOldImage]; if ([[arrayOfNewImageGrids objectAtIndex:index]isEqual:[arrayOfOldImageGrids objectAtIndex:index]]) { NSLog(@"Index=%d",index); } NSLog(@"newGridArray=%@",[arrayOfNewImageGrids objectAtIndex:index]); NSLog(@"oldGridArray=%@",[arrayOfOldImageGrids objectAtIndex:index]); index++; } } 问题是当比较结果为FALSE时它会到达,尽管在数组中是同一个大图像的裁剪图像。
在此先感谢....

1 个答案:

答案 0 :(得分:1)

我非常怀疑UIImage实施-isEqual:(和-hash)来比较图像内容!几乎可以肯定,它们只依赖于指针标识的标准NSObject等式语义(它们只与自身相等)。

如果你想比较两张图片是否相等,你必须自己做。比较所有元数据属性。然后,在确定所有元数据相等后,获取基础图像位并使用memcmp()NSData进行比较。