Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Xcode 使用相同颜色背景绘制图像时的颜色偏移_Xcode_Macos_Rgb_Nsbitmapimagerep - Fatal编程技术网

Xcode 使用相同颜色背景绘制图像时的颜色偏移

Xcode 使用相同颜色背景绘制图像时的颜色偏移,xcode,macos,rgb,nsbitmapimagerep,Xcode,Macos,Rgb,Nsbitmapimagerep,我所期望的是,输出将是1440x900背景上的图像,具有与图像相同的背景颜色(不是红色),这会发生,但颜色上有一个微小的变化,使得背景颜色比它应该的颜色更亮或更暗。这与色彩空间有关,但我不知道该如何处理 NSURL *myImage; NSString *tempDir; tempDir = @"/Users/Maxime/Documents/"; myImage = [NSURL fileURLWithPath: [tempDir stringByAppendingString:@"344e

我所期望的是,输出将是1440x900背景上的图像,具有与图像相同的背景颜色(不是红色),这会发生,但颜色上有一个微小的变化,使得背景颜色比它应该的颜色更亮或更暗。这与色彩空间有关,但我不知道该如何处理

NSURL *myImage;
NSString *tempDir;

tempDir = @"/Users/Maxime/Documents/";
myImage = [NSURL fileURLWithPath: [tempDir stringByAppendingString:@"344ehb5.png"]];
NSLog(@"soundFile: %@", myImage);

NSImage *image = [[NSImage alloc] initWithContentsOfURL:myImage];

NSArray *upper = [self getRGBAsFromImage:image atX:5 andY:5 count:1];

NSColor *color = [upper lastObject];

    NSRect imgRect = NSMakeRect(0.0, 0.0, 1400.0, 900.0);
NSSize imgSize = imgRect.size;

NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
                                   initWithBitmapDataPlanes:NULL
                                   pixelsWide:imgSize.width
                                   pixelsHigh:imgSize.height
                                   bitsPerSample:8
                                   samplesPerPixel:4
                                   hasAlpha:YES
                                   isPlanar:NO
                                   colorSpaceName:NSCalibratedRGBColorSpace
                                   bitmapFormat:NSAlphaFirstBitmapFormat
                                   bytesPerRow:0
                                   bitsPerPixel:0] autorelease];


// set offscreen context
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];


//Set color of drawing, and fill the rectangle, so we can see it's boundaries.
[color setFill];
NSRectFill(imgRect);


//add image
NSRect shot = NSMakeRect(500.0, 300.0, 400.0, 300.0);
[image drawInRect:shot fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];


// create an NSImage and add the rep to it
NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[img addRepresentation:offscreenRep];
NSData *imagef = [[img TIFFRepresentation] retain];    
[imagef writeToFile: @"/Users/Maxime/Desktop/test.png" atomically: NO];
我使用以下代码获取像素数据:

- (NSArray*)getRGBAsFromImage:(NSImage*)image atX:(int)xx andY:(int)yy count:(int)count
{
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

    // create the image somehow, load from file, draw into it...
    CGImageSourceRef source;
    source = CGImageSourceCreateWithData((CFDataRef)[image TIFFRepresentation], NULL);

    // First get the image into your data buffer
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
    for (int ii = 0 ; ii < count ; ++ii)
    {
        CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
        CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
        CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
        CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
        byteIndex += 4;

        NSColor *acolor  = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];

        [result addObject:acolor];
    }

    free(rawData);

    return result;
}
-(NSArray*)getRGBAsFromImage:(NSImage*)图像atX:(int)xx安迪:(int)yy计数:(int)计数
{
NSMUTABLEARRY*result=[NSMUTABLEARRY阵列容量:计数];
//以某种方式创建图像,从文件加载,绘制到其中。。。
CGImageSourceRef源;
source=CGImageSourceCreateWithData((CFDataRef)[image-tiffresentation],NULL);
//首先将图像放入数据缓冲区
CGImageRef imageRef=CGImageSourceCreateMageAtIndex(源,0,空);
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
unsigned char*rawData=(unsigned char*)calloc(高度*宽度*4,sizeof(unsigned char));
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
//现在,您的rawData包含RGBA8888像素格式的图像数据。
int byteIndex=(bytesPerRow*yy)+xx*bytesperpoixel;
对于(int ii=0;ii