简体   繁体   English

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

[英]How to increase/decrease an Unsigned Char?

I am trying to modify pixel values (8 bits per channel RGBA) by numerically increasing/decreasing the values by a certain amount. 我试图通过在数值上增加/减少一定量来修改像素值(每通道RGBA 8位)。 How can I do this in Objective-C or C? 我怎么能在Objective-C或C中做到这一点? The following code generates a " Error: EXC_BAD_ACCESS " everytime. 以下代码每次都会生成“ 错误: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;

}

and even 乃至

// 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;

}

Sidenote: rawData is a data buffer of type unsigned char 旁注: rawData是unsigned char类型的数据缓冲区

It's possible that you're overrunning the end of your allocated buffer, and that's why you're getting the access violation. 您可能超出了已分配缓冲区的末尾,这就是您获得访问冲突的原因。 That most likely means that your math is wrong in the allocation, or your rawData pointer is of the wrong type. 这很可能意味着您的数学分配错误,或者您的rawData指针类型错误。

If you are accessing the raw data of a loaded UIImage, it might be mapped into memory read-only. 如果要访问已加载的UIImage的原始数据,它可能会以只读方式映射到内存中。 You'd need to copy the data into a buffer that you allocated, most likely. 您需要将数据复制到您分配的缓冲区中,最有可能。

Hmm... What's rawdata ? 嗯......什么是rawdata Maybe it's a const type which you can not modify? 也许这是一个你无法修改的const类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM