2015-09-03 20 views
0

我正在尝试使用Cocoa处理一个大的(8000×8000)图像。当使用NSImage中,下面的代码会立即使用高达约1GB的RAM:NSImage与NSBitmapImageRep:海量内存使用差异?

var outputImage = NSImage(size: NSMakeSize(8000, 8000)) 
outputImage.lockFocus() 
// drawing operations here 

但使用NSBitmapImageRep时,只有几百MB的应用于:

var outputRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 8000, pixelsHigh: 8000, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0) 
var context = NSGraphicsContext(bitmapImageRep: outputRep!) 
NSGraphicsContext.saveGraphicsState() 
NSGraphicsContext.setCurrentContext(context) 
// drawing operations here 

如果我的数学是正确的,8000 ×8000图像应该用完(8000×8000×4÷1024÷1024)= 244MB,这符合NSBitmapImageRep的内存使用。

为什么NSImage使用4倍的内存?

回答

0

糟糕!我错过了4倍内存使用的重要性。事实证明,NSImage使用点而不是像素,这意味着在Retina设备上,所有NSImages实际上都将使用2×像素分辨率。这意味着我的8000×8000点(16000×16000像素)图像的实际内存占用量为977MB,这与我的结果一致。