Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检查是否比较UIS图像_Ios_Uiimageview_Uiimage - Fatal编程技术网

Ios 检查是否比较UIS图像

Ios 检查是否比较UIS图像,ios,uiimageview,uiimage,Ios,Uiimageview,Uiimage,我使用这个代码,但是速度很慢。还有别的办法吗? 我尝试使用indexOfObject和containsObject方法来处理图像数组,但对我来说不起作用 BOOL haveDublicate = NO; UIImage *i = [ImageManager imageFromPath:path]; NSArray *photoImages = [ImageManager imagesFromPaths:photoPaths]; fo

我使用这个代码,但是速度很慢。还有别的办法吗? 我尝试使用indexOfObject和containsObject方法来处理图像数组,但对我来说不起作用

        BOOL haveDublicate = NO;
        UIImage *i = [ImageManager imageFromPath:path];
        NSArray *photoImages = [ImageManager imagesFromPaths:photoPaths];
        for (UIImage *saved in photoImages)
        {
            if ([ UIImagePNGRepresentation( saved ) isEqualToData:
                 UIImagePNGRepresentation( i ) ])
            {
                haveDublicate = YES;
            }
        }

我想你应该先检查一下图像的大小。如果两个图像的大小和比例相等,则表示相等,而不是图像的PNG表示。这会快得多。该链接显示如何获取像素数据。要进行比较,请使用memcmp

对该帖子稍作修改:

NSData *rawDataFromUIImage(UIImage *image)
{
    assert(image);

    // Get the image into the data buffer
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int byteSize = height * width * 4;
    unsigned char *rawData = (unsigned char*) malloc(byteSize);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                    bitsPerComponent, bytesPerRow, colorSpace,
                    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    return [NSData dataWithBytes:rawData length:byteSize];
}

关于为什么速度更快:UIImagePNGRepresentation 1获取原始二进制数据,然后2将其转换为PNG格式。跳过第二步只能提高性能,因为这比只执行第1步要复杂得多。在这个例子中,memcmp比其他任何东西都快。

与其逐字节查看数据,为什么不检查数据的大小,只有当它们相等时,才检查isEqualToData:?-isEqualToData:已经这样做了。慢的部分是UIImagePNGRE演示文稿。但是在检查数据之前比较图像的大小可能是个好主意。。。也许你的意思是,无论如何……谢谢我使用IsequalToData。另外,请确保你在循环之外获得“I”的像素数据,因为这不会改变。在上面的原始代码中,每次都会得到i的png数据,因此只要将uiimagepngrei移出循环,速度就会立即提高一倍。另一种优化方法是,在找到重复的图像后,中断循环并添加一个“break”语句,否则,在找到重复的图像后,您将继续检查所有其他图像。@NigelG没错。这将使其速度提高100%。