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倍的内存?