CIColorCube inputCubeData不是预期的长度

时间:2014-04-17 22:15:26

标签: objective-c ciimage

我正在尝试使用CIColorCube过滤器。 我已经从Apple页面(https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_filer_recipes/ci_filter_recipes.html#//apple_ref/doc/uid/TP30001185-CH4-SW1)复制了粘贴代码,但我无法运行代码。 我修改了代码,我设法摆脱了一些错误,但由于长度错误,我无法编译。 有人可以帮忙吗?

-(NSImage*)getImageFiltered :(NSURL*)theImage forValue:(double)value
{
CIImage * ciImage = [CIImage imageWithContentsOfURL:theImage];
// Allocate memory
const unsigned int size = 64;
float *cubeData = (float *)malloc (size * size * size * sizeof (float) * 4);
float rgb[3];

// Populate cube with a simple gradient going from 0 to 1
for (int z = 0; z < size; z++){
    rgb[2] = ((double)z)/(size-1)+(value/100); // Blue value
    for (int y = 0; y < size; y++){
        rgb[1] = ((double)y)/(size-1)+(value/100); // Green value
        for (int x = 0; x < size; x ++){
            float alpha=1.0f;
            c[0] = rgb[0] * alpha;
            c[1] = rgb[1] * alpha;
            c[2] = rgb[2] * alpha;
            c[3] = alpha;
            c += 4;
                        }
    }
}
// Create memory with the cube data
NSData *data = [NSData dataWithBytesNoCopy:cubeData
                                    length:sizeof(*cubeData)
                              freeWhenDone:YES];
CIFilter *colorCube = [CIFilter filterWithName:@"CIColorCube"];
[colorCube setValue:@(size) forKey:@"inputCubeDimension"];
// Set data for cube
[colorCube setValue:data forKey:@"inputCubeData"];
[colorCube setValue:ciImage forKey:kCIInputImageKey];


 CIImage *result = [colorCube valueForKey: kCIOutputImageKey];
 NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:result];
 NSImage *nsImage = [[NSImage alloc] initWithSize:rep.size];
 [nsImage addRepresentation:rep];
 return nsImage;
} 

的Dario

1 个答案:

答案 0 :(得分:2)

length:来电的dataWithBytesNoCopy:值必须类似于

size_t cubeDataSize = size * size * size * sizeof ( float ) * 4;

但是您的代码还有其他一些问题;看看这个工作示例:

https://github.com/vhbit/ColorCubeSample/blob/master/ColorCube/ViewController.m