我在试着用
NSBitmapImageRep.bitmapImageRepByConvertingToColorSpace(NSColorSpace.genericRGBColorSpace(), renderingIntent: NSColorRenderingIntent.Perceptual);要将NSImage转换为openGL可以处理的格式,它可以工作(与bitmapImageRepByRetaggingWithColorSpace()不同),但我得到一个错误:
<Error>: The function ‘CGContextClear’ is obsolete and will be removed in an upcoming update. Unfortunately, this application, or a library it uses, is using this obsolete function, and is thereby contributing to an overall degradation of system performance.这有点没用,因为它是苹果自己的代码,我似乎找不到bitmapImageRepByConvertingToColorSpace()的替代方法,也没有注意到它也被弃用了。
编辑:
var image=NSImage(size: frame);
image.lockFocus();
//println("NSImage: \(image.representations)");
string.drawAtPoint(NSMakePoint(0.0,0.0), withAttributes: attribs);
var bitmap2=NSBitmapImageRep(focusedViewRect: NSMakeRect(0.0,0.0,frame.width,frame.height))?;
image.unlockFocus();
bitmap=bitmap2!.bitmapImageRepByConvertingToColorSpace(NSColorSpace.genericRGBColorSpace(), renderingIntent: NSColorRenderingIntent.Perceptual);发布于 2014-12-19 00:25:54
出于某种原因,这个问题引起了我的兴趣。看起来解决方案是将焦点锁定在NSImage上,然后使用NSBitmapImageRep(focusedViewRect:)创建NSBitmapImageRep。
我尝试重新创建您的情况(据我所知),然后使用以下代码创建一个NSBitmapImageRep:
var img:NSImage = NSImage(size: NSMakeSize(200,200))
img.lockFocus()
let testString:NSString = "test String"
testString.drawAtPoint(NSMakePoint(10,10), withAttributes: nil)
img.unlockFocus()
println("img Description = \(img.description)")我得到了以下描述:
img描述= NSImage 0x608000067ac0 Size={200,200} Reps=( "NSCGImageSnapshotRep:0x61000007cf40 cgImage=CGImage 0x6100001a2bc0")
然后,我将此代码扩展为:
var img:NSImage = NSImage(size: NSMakeSize(200,200))
img.lockFocus()
let testString:NSString = "test String"
testString.drawAtPoint(NSMakePoint(10,10), withAttributes: nil)
img.unlockFocus()
println("img Description = \(img.description)")
img.lockFocus()
var bitmapRep:NSBitmapImageRep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0.0, 0.0, img.size.width, img.size.height))!
img.unlockFocus()
println("bitmap data planes = \(bitmapRep.bitmapData)")
println("bitmap pixels wide = \(bitmapRep.size.width)")
println("bitmap pixels high = \(bitmapRep.size.height)")
println("bits per sample = \(bitmapRep.bitsPerSample)")
println("samples per pixel = \(bitmapRep.samplesPerPixel)")
println("has alpha = \(bitmapRep.alpha)")
println("is planar = \(bitmapRep.planar)")
println("color space name = \(bitmapRep.colorSpace)")
println("bitmap format = \(bitmapRep.bitmapFormat)")
println("bytes per row = \(bitmapRep.bytesPerRow)")
println("bits per pixel = \(bitmapRep.bitsPerPixel)")NSBitmapImageRep参数的输出为:
位图数据平面= 0x000000010b6ff100
位图像素宽度= 200.0
位图像素高= 200.0
每个样本的位数=8
每像素采样数=4
has alpha = true
是平面= false
色彩空间名称=彩色LCD色彩空间
位图格式= C.NSBitmapFormat
每行字节数= 800
每像素位数= 32
发布于 2014-12-20 14:21:48
我在下面发布了一些新的代码。我已经创建了两个和您的代码一样的NSBitMapImageRep (除了我使用的方法bitmapImageRepByRetaggingWithColorSpace),并且我将它们都导出到一个PNG文件中。我在两个PNG文件中得到了相同的图像。
但我想知道几件事。首先,两个测试的不同之处不仅在于OS版本,而且硬件产生了不同的色彩空间。您应该检查以确保bitmap2不为空。
其次,我想知道OpenGL是否关心。如果bitmap2不是nil,并且您传递了OpenGL bitmap2.bitmapData,它能工作吗?
我的新代码(删除大部分println):
var img:NSImage = NSImage(size: NSMakeSize(200,200))
img.lockFocus()
let testString:NSString = "test String"
let font:NSFont = NSFont(name: "AppleCasual", size: 18.0)!
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.LeftTextAlignment
let textColor:NSColor = NSColor(calibratedRed: 1.0, green: 0.0, blue: 1.0, alpha: 1.0)
let attribs:NSDictionary = [NSFontAttributeName: font,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle]
testString.drawAtPoint(NSMakePoint(0.0, 0.0), withAttributes: attribs)
img.unlockFocus()
println("img Description = \(img.description)")
img.lockFocus()
var bitmapRep:NSBitmapImageRep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0.0, 0.0, img.size.width, img.size.height))!
img.unlockFocus()
let pngPath:String = "TestStringBeforeChange.png"
let imageProps = [NSImageCompressionFactor: NSNumber(float: 1.0)]
let outputImageData = bitmapRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: imageProps)
let fileSavePanel = NSSavePanel()
fileSavePanel.nameFieldStringValue = pngPath
var savePanelReturn = fileSavePanel.runModal()
if savePanelReturn == NSFileHandlingPanelOKButton
{
var theFileURL = fileSavePanel.URL
var saveStatus = outputImageData?.writeToURL(theFileURL!, atomically: true)
}
let bitmapRep2 = bitmapRep.bitmapImageRepByRetaggingWithColorSpace(NSColorSpace.genericRGBColorSpace())
let pngPath2:String = "TestStringAfterChange.png"
let outputImageData2 = bitmapRep2!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: imageProps)
let fileSavePanel2 = NSSavePanel()
fileSavePanel2.nameFieldStringValue = pngPath2
var savePanel2Return = fileSavePanel2.runModal()
if savePanel2Return == NSFileHandlingPanelOKButton
{
var theFileURL2 = fileSavePanel2.URL
var saveStatus2 = outputImageData2?.writeToURL(theFileURL2!, atomically: true)
}发布于 2014-12-20 17:46:51
很抱歉所有的帖子,但我不想让它过去,这很有趣。
我创建了3个PNG文件:第一个使用原始的NSBitmapImageRep,第二个使用使用bitmapImageRepByRetaggingWithColorSpace创建的NSBitmapImageRep,第三个使用使用bitmapImageRepByConvertingToColorSpace创建的NSBitmapImageRep (当然,我在使用这个函数时收到了系统警告)。
在预览的检查器中查看这三个PNG文件,它们都是RGB颜色模型;只有第一个文件的colorSynch配置文件与第二个和第三个文件的配置文件不同。所以我想也许这些文件的bitmapData没有区别。
然后我写了一些代码来比较bitmapData:
var firstRepPointer:UnsafeMutablePointer<UInt8> = bitmapRep.bitmapData
var secondRepPointer:UnsafeMutablePointer<UInt8> = bitmapRep2!.bitmapData
var firstByte:UInt8 = 0
var secondByte:UInt8 = 0
for var i:Int = 0; i < 200; i++
{
for var j:Int = 0; j < 200; j++
{
for var k:Int = 0; k < 4; k++
{
memcpy(&firstByte, firstRepPointer, 1)
firstRepPointer += 1
memcpy(&secondByte, secondRepPointer, 1)
secondRepPointer += 1
if firstByte != secondByte {println("firstByte = \(firstByte) secondByte = \(secondByte)")}
}
}
}正如我所料,当我将原始位图表示的bitmapData与使用bitmapImageRepByRetaggingWithColorSpace创建的位图表示的bitmapData进行比较时,两者没有区别。
然而,当我将来自原始位图表示的bitmapData与来自用bitmapImageRepByConvertingToColorSpace创建的位图表示的数据进行比较时,它变得更加有趣。
数据中有很多不同之处。差异很小,但有很大的差异。
尽管我在PNG文件中看不到任何差异,但底层数据已经被bitmapImageRepByConvertingToColorSpace更改了。
综上所述,我认为您应该能够将.bitmapData从这些位图表示中的任何一个传递到OpenGL纹理。使用bitmapImageRepByConvertingToColorSpace创建的位图表示中的数据可能会创建一个看起来不同的纹理,但它应该是有效数据。
https://stackoverflow.com/questions/27548722
复制相似问题