1

我正在尝试将两个图像合并为背景图像。这在非 Retina Mac 上运行良好。

生成的图像具有我原始背景图像的大小。但在 MacBook Pro Retina 上,生成的图像会翻倍。我读到应该使用它imageWithSize来解决这个问题,因为 10.8 / Retina 支持图像不再以像素为单位;我们知道它的要点。

我不知道我应该如何更改我的代码来解决这个问题。

到目前为止我所拥有的:

    NSSize size =  background.size  ;
    GLsizei width = (GLsizei)size.width;
    GLsizei height = (GLsizei)size.height;
    NSBitmapImageRep* imgRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:width*3 bitsPerPixel:0];  

#ifdef __MAC_10_8

    NSImage* backgroundWrapper = [NSImage imageWithSize:size flipped:YES drawingHandler:^(NSRect dstRect){ return [imgRep drawInRect:dstRect]; }];
    NSPoint backgroundPoint;
    backgroundPoint.x = 0;
    backgroundPoint.y = 0;
    [backgroundWrapper lockFocusFlipped:YES];
    [background drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [firstImage firstImage fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [backgroundWrapper unlockFocus];

    NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[backgroundWrapper TIFFRepresentation]];
    [backgroundWrapper addRepresentation:bmpImageRep];
#else
    [background lockFocusFlipped:YES];
    [firstImage drawAtPoint:firstImagePoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [background unlockFocus];
    NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[background TIFFRepresentation]];
    [background addRepresentation:bmpImageRep];
#endif

    NSData *data = [bmpImageRep representationUsingType: NSPNGFileType properties: nil];

我怎样才能让它在 Retina 显示器上工作?

编辑

我也试过这个:

- (NSImage )drawImage:(NSImage)background with:(NSImage*)firstImage { 

    return [NSImage imageWithSize:NSMakeSize(3200, 2000) flipped:NO drawingHandler:^BOOL(NSRect dstRect) { 
        NSPoint backgroundPoint; backgroundPoint.x = 0; 
        backgroundPoint.y = 0; 
        [background drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 
        [firstImage drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 
        return true; 
    }];
}

但生成的图像是 6400x4000px

4

0 回答 0