如何增加/减少无符号字符?

时间:2009-08-30 06:11:17

标签: iphone c objective-c bitmap unsigned-char

我试图通过数值增加/减少一定量的值来修改像素值(每通道8位RGBA)。我怎么能在Objective-C或C中做到这一点?以下代码每次都会生成“错误:EXC_BAD_ACCESS ”。

// Try to Increase RED by 50
    for(int i = 0; i < myLength; i += 4) {

        //NSLog prints the values FINE as integers
            NSLog(@"(%i/%i/%i)", rawData[i], rawData[i+1], rawData[i+2]);

            //But for some reason I cannot do this
        rawData[i]+=50;

}

甚至

// Try to set RED to 50
    for(int i = 0; i < myLength; i += 4) {

            //I cannot even do this...
        unsigned char newVal = 50;
        rawData[i] = 50;

}

旁注: rawData是unsigned char

类型的数据缓冲区

2 个答案:

答案 0 :(得分:4)

您可能会超出已分配缓冲区的末尾,这就是您获得访问冲突的原因。这很可能意味着您的数学分配错误,或者您的rawData指针类型错误。

如果要访问已加载的UIImage的原始数据,它可能会以只读方式映射到内存中。您需要将数据复制到您分配的缓冲区中,最有可能。

答案 1 :(得分:0)

嗯......什么是rawdata?也许它是const类型,您无法修改?

相关问题