在Retina Macs上合并Cocoa中的2张图像

时间:2012-12-27 21:39:11

标签: macos cocoa nsimage

我正在尝试将两个图像合并到背景图像中。这适用于非Retina Macs。

生成的图像具有原始背景图像的大小。但在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

0 个答案:

没有答案