0

vImageHistogramCalculation在我当前的应用程序中使用了计算图像的直方图,在某些情况下我得到了EXC_BAD_ACCESS 。我经历了如下案例 -

- (void)histogramForImage:(UIImage *)image {
    vImage_Buffer inBuffer;
    CGImageRef img = image.CGImage;

    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);

    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    vImagePixelCount histogram[4][8] = {{0}};
    vImagePixelCount *histogramPointers[4] = { &histogram[0][0], &histogram[1][0], &histogram[2][0], &histogram[3][0] };
    vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogramPointers, 8, 0, 255, kvImageNoFlags);

    if (error) {
        NSLog(@"error %ld", error);
    }

    CGDataProviderRelease(inProvider);
}
  1. 我使用了PNG格式的 iPhone 相机胶卷中的图像,并在上面的代码中手动放入 bundle 中,它工作正常。

  2. 我对 iPhone 相机胶卷中JPG格式的图像使用了相同的代码,然后出现 EXC_BAD_ACCESS 错误。

  3. 我尝试使用照片框架从相机胶卷中获取图像,然后传递给相同的代码,然后我也收到 EXC_BAD_ACCESS 错误。

我真正需要的是找到iPhone 相机胶卷所有图像的直方图。所以我无法找出为什么上面的代码对一种图像格式工作正常而对另一种图像格式失败。还有其他原因导致崩溃吗?

编辑 1-我得出的不是图像格式,它对于某些 JPG 图像也可以正常工作,但在某些情况下仍然会崩溃。我应该如何弄清楚?

参考

https://developer.apple.com/library/mac/documentation/Performance/Reference/vImage_histogram/#//apple_ref/c/func/vImageHistogramCalculation_ARGBFFFF

使用 vImageHistogramCalculation 计算图像的直方图

4

1 回答 1

1

vImageHistogramCalculation_ARGBFFFF 用于四通道浮点数据。您从数据提供程序中获取的数据是 RGB 或 RGBA 8 位整数数据的可能性非常高。检查 CGImageRef 以了解图像数据的存储格式。

如果您想从 CGImageRef 中获取特定的数据格式,可以调用 vImageBuffer_InitWithCGImage()。

于 2015-10-16T22:33:53.073 回答