Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
从iPhone上的UIImage获取像素数据,为什么红色的总是255?_Iphone_Objective C_Cocoa Touch_Uiimage - Fatal编程技术网

从iPhone上的UIImage获取像素数据,为什么红色的总是255?

从iPhone上的UIImage获取像素数据,为什么红色的总是255?,iphone,objective-c,cocoa-touch,uiimage,Iphone,Objective C,Cocoa Touch,Uiimage,我们一直在研究并以不同的运气使用这种方法: -(RGBPixel*)bitmap{ CGImageRef cgImage = [image CGImage]; CGDataProviderRef provider = CGImageGetDataProvider(cgImage); bitmapData = CGDataProviderCopyData(provider); [self setPixelByteData: malloc( CFDataGe

我们一直在研究并以不同的运气使用这种方法:

-(RGBPixel*)bitmap{

    CGImageRef cgImage = [image CGImage];

    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);

    bitmapData = CGDataProviderCopyData(provider);  

   [self setPixelByteData: malloc( CFDataGetLength(bitmapData) )];

    CFDataGetBytes( bitmapData, CFRangeMake( 0, CFDataGetLength( bitmapData ) ), pixelByteData );

    pixelData   = (RGBPixel*) pixelByteData;

    colorLabel.text = [[NSString alloc]initWithFormat:@"Pixel data: red (%i), green (%i), blue (%i).", pixelData[100].red, pixelData[100].green, pixelData[100].blue] ;

    return pixelData;
}

唯一不能像我所希望的那样工作的是红色像素数据:它总是显示255,而绿色和蓝色的表现与预期的一样。我做错了什么?

正如@Nick所暗示的,你确定你知道图像的格式吗?bitsPerComponent、bitsPerPixel、bytesPerRow、颜色空间?数据可以是int或float格式。它可以是RGB、RGBA或ARGB


除非您自己根据原始数据创建图像,否则您可能无法确定。您可以使用诸如CGImageGetBitmapInfo()CGImageGetAlphaInfo()CGImageGetColorSpace()之类的函数来查找。您确定这不是alpha通道吗?谢谢,看起来格式是ARGB,而不是我预期的RGB。所以红色的不总是255,阿尔法是;)