Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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 CGContextDrawImage跨设备的字节值不一致?_Iphone_Objective C_Ios_Bitmap_Core Graphics - Fatal编程技术网

Iphone CGContextDrawImage跨设备的字节值不一致?

Iphone CGContextDrawImage跨设备的字节值不一致?,iphone,objective-c,ios,bitmap,core-graphics,Iphone,Objective C,Ios,Bitmap,Core Graphics,我试图比较两个图像(实际上是在较大的图像中定位较小的“子图像”),并使用下面提供的方法加载图像 下面的代码现在包含一个testing for循环,它汇总了所有单个字节值。我发现这个总和和相应的字节数是不同的,这取决于它运行的设备。我的问题是为什么会这样 // Black and white configuration: CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); NSUInteger bytesPerPixel = 1

我试图比较两个图像(实际上是在较大的图像中定位较小的“子图像”),并使用下面提供的方法加载图像

下面的代码现在包含一个testing for循环,它汇总了所有单个字节值。我发现这个总和和相应的字节数是不同的,这取决于它运行的设备。我的问题是为什么会这样

// Black and white configuration:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
NSUInteger bytesPerPixel = 1;
NSUInteger bitsPerComponent = 8;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

// Image
CGImageRef imageRef = [[UIImage imageNamed:@"image.jpg"] CGImage];
NSUInteger imageWidth = CGImageGetWidth(imageRef);
NSUInteger imageHeight = CGImageGetHeight(imageRef);
NSUInteger imageSize = imageHeight * imageWidth * bytesPerPixel;
NSUInteger imageBytesPerRow = bytesPerPixel * imageWidth;

unsigned char *imageRawData = calloc(imageSize, sizeof(unsigned char));
CGContextRef imageContext = CGBitmapContextCreate(imageRawData, imageWidth, imageHeight, bitsPerComponent,
                                                  imageBytesPerRow, colorSpace, bitmapInfo);

// Draw the actual image to the bitmap context
CGContextDrawImage(imageContext, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
CGContextRelease(imageContext);


NSUInteger sum = 0;
for (int byteIndex = 0; byteIndex < imageSize; byteIndex++)
{
    sum += imageRawData[byteIndex];
}

NSLog(@"Sum: %i", sum); // Output on simulator:    Sum: 18492272
                        // Output on iPhone 3GS:   Sum: 18494036
                        // Output on another 3GS:  Sum: 18494015
                        // Output on iPhone 4:     Sum: 18494015


free(imageRawData);
CGColorSpaceRelease(colorSpace);
//黑白配置:
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceGray();
NSU整数字节/像素=1;
NSU整数比特分量=8;
CGBitmapInfo bitmapInfo=kCGBitmapByteOrderDefault;
//形象
CGImageRef imageRef=[[UIImage IMAGENAME:@“image.jpg”]CGImage];
NSU整数imageWidth=CGImageGetWidth(imageRef);
NSU整数imageHeight=CGImageGetHeight(imageRef);
NSUTEGER imageSize=图像高度*图像宽度*字节/像素;
NSUInteger imageBytesPerRow=bytesPerPixel*图像宽度;
unsigned char*imageRawData=calloc(imageSize,sizeof(unsigned char));
CGContextRef imageContext=CGBitmapContextCreate(imageRawData、imageWidth、imageHeight、bitsPerComponent、,
imageBytesPerRow、颜色空间、位图信息);
//将实际图像绘制到位图上下文中
CGContextDrawImage(imageContext,CGRectMake(0,0,imageWidth,imageHeight),imageRef);
CGContextRelease(imageContext);
整数和=0;
对于(int-byteIndex=0;byteIndex
我假设“设备灰色”颜色空间因设备而异。尝试使用独立于设备的颜色空间

这些设备是否都运行相同版本的操作系统?另一种可能性(除了颜色空间,有人已经提到)是JPG解码库可能会有细微的不同。由于JPEG是一种有损图像格式,不同的解码器产生的位图位不相等并非不可想象。考虑到iOS用户界面中大量使用图像,我们可以合理地假设JPG解码器将不断调整以获得最佳性能

我甚至相信,在不同型号的设备(即不同的处理器)上运行的同一操作系统版本之间,如果JPG解码器有多个版本,每个版本都针对特定的CPU进行了大量优化,结果可能会不一样,尽管这不能解释同一型号的两个设备之间的差异,使用相同的操作系统

您可以尝试使用无损格式的图像重新运行该实验

还值得指出的是,为CGBitmapContext提供自己的支持内存,但不特别考虑单词对齐可能会导致性能低下。例如,您有:

NSUInteger imageBytesPerRow = bytesPerPixel * imageWidth;

如果imageBytesPerRow不是CPU本机字长的倍数,您将获得次优性能。

Hmm,我刚刚尝试用CGColorSpaceCreateDeviceGray()替换cgColorSpaceCreateCaliblatedGray((float[3]){1.0,1.0,1.0},(float[3]){0.0,0.0,0.0},1.0),这应该是设备独立的,结果是相同的,总和不同(我得到的数字与我的问题中提到的相同)…我认为你是对的!实际上,我的示例中的两个结果是相同的,iPhone3GS和iPhone4都有相同的iOS版本5.0.1