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