【发布时间】:2017-12-29 19:54:56
【问题描述】:
我又被一个看似简单的问题困住了。
我将 JPEG 文件加载到 CGImage 中。我得到了正确的宽度和高度值(以像素为单位),并且能够在 ImageView 控制器中显示“myImage”。但我想在这张图片上添加一些图形,发现我应该把它放到一个 NSImage 中。所以我做了,但得到了不同的(比例)宽度和高度值:595.08 代替 1653,841.68 代替 2338。
我试图从 CGContext 'gc' 创建一个 NSCGContext 用于绘制(一条简单的线和一个矩形),结果是“可选类型 'CGContext 的值?'没有打开,你的意思是用'!'还是'?'?”...我迷路了...
// with NSData
//
let imageAsData = try Data(contentsOf: chosenFiles[0])
let imageProvider = CGDataProvider(data: imageAsData as CFData)
var myImage = CGImage(jpegDataProviderSource: imageProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let imageWidth = myImage!.width
let imageHeight = myImage!.height
// with NSImage, now
//
let imageAsNSImage=NSImage(contentsOf: chosenFiles[0])
let imageSize=imageAsNSImage?.size // ---> 0.36 * pixels
// creating a CG context and drawing
//
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let gc = CGContext(data: nil, width: imageWidth, height: imageHeight, bitsPerComponent: 8, bytesPerRow: 0,space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)
let NSGContext = NSGraphicsContext(cgContext: gc, flipped: true)
let currentContext = NSGraphicsContext.current() // Cocoa GC object appropriate for the current drawing environment
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.current = NSGContext
NSGContext?.beginPath()
NSGContext?.setStrokeColor(redColor)
NSGContext?.setLineWidth(50.0)
NSGContext?.move(to: targetStart)
NSGContext?.addLine(to: targetEnd)
NSGContext?.setStrokeColor(grayColor)
NSGContext?.setFillColor(grayColor)
NSGContext?.addRect(ROIRect)
NSGContext?.closePath()
NSGContext.restoreGraphicsState()
imageAsNSImage?.draw(at: NSZeroPoint, from: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1.0)
imageAsNSImage?.unlockFocus()
NSGraphicsContext.setcurrent(currentContext)
myImageView.image = imageAsNSImage // image & drawings should show in View
【问题讨论】:
-
没有人知道你在哪个类下编写代码。你创建一个 CGImage 对象是为了什么目的?
-
我创建了一个 CGImage 以便以后访问像素(此处未显示)。我需要使用概率霍夫变换来检测 ROI(感兴趣区域)内的一条线,它已经或多或少地工作了......无论如何,NSImage 是在我想要绘制的 ImageView 中显示正在处理的图像目标线(黄色)和 ROI(灰色)。扫描图像并且必须为检测到的线重新调整/重新缩放/翻译以适合目标线。这样做是为了改善最佳标记识别的结果。