- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码来创建一个 200x200 像素的圆图像。这不是在屏幕上使用,所以我在锁定焦点和绘图之前手动将 NSBitmapImageRep 添加到图像。
let ovalSize = CGSize(width: 200, height: 200)
let ovalPath = NSBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: ovalSize))
let ovalImage = NSImage(size: ovalSize)
let imageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 200, pixelsHigh: 200, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: true, colorSpaceName: .deviceRGB, bitmapFormat: [.alphaFirst, .thirtyTwoBitLittleEndian], bytesPerRow: 0, bitsPerPixel: 0)
ovalImage.addRepresentation(imageRep!)
ovalImage.lockFocus()
NSColor(deviceWhite: 1, alpha: 1).setFill()
ovalPath.fill()
ovalImage.unlockFocus()
但是,如果我快速查看 ovalImage
,它已以 400x400 呈现,并且打印 ovalImage
的表示会产生以下内容:
[<NSCGImageSnapshotRep:0x60000175bf00 cgImage=<CGImage 0x101612d30> (DP)
<<CGColorSpace 0x60000260cba0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; Color LCD)>
width = 400, height = 400, bpc = 8, bpp = 32, row bytes = 1600
kCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little | kCGImagePixelFormatPacked
is mask? No, has masking color? No, has soft mask? No, has matte? No, should interpolate? Yes>]
lockFocus
的文档提到,如果一个表示不可用,将添加一个表示,所以我很困惑为什么要替换我添加的那个?
最佳答案
我想你需要的是"Drawing Offscreen Images Using a Block-Based Drawing Method to Support High Resolution Displays" .
你可以这样做:
let ovalPath = NSBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: NSMakeSize(200, 200)))
let ovalImage = NSImage(size: NSMakeSize(200, 200), flipped: false) { (rect) -> Bool in
NSColor(deviceWhite: 1, alpha: 1).setFill()
ovalPath.fill()
return true
}
或
let ovalSize = CGSize(width: 200, height: 200)
let ovalPath = NSBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: NSMakeSize(200, 200)))
let imageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 200, pixelsHigh: 200, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: true, colorSpaceName: .deviceRGB, bitmapFormat: [.alphaFirst, .thirtyTwoBitLittleEndian], bytesPerRow: 0, bitsPerPixel: 0)
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: imageRep!)
NSColor(deviceWhite: 1, alpha: 1).setFill()
ovalPath.fill()
NSGraphicsContext.restoreGraphicsState()
let ovalImage = NSImage(size: ovalSize)
ovalImage.addRepresentation(imageRep!)
我不知道您添加的表示被替换的原因,但您应该 treat NSImage and its image representations as immutable objects .
关于swift - 在 NSImage 上使用 lockFocus 时如何强制使用非视网膜图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54560814/
我有一个矩形 NSImage A,我想缩放以嵌入到方形透明图像 B 中,保持 A 的比例。所以,最后我会得到一个带有矩形的方形图像。 我该如何构图?我的意思是,如何在另一个 NSImage 上绘制一个
我有一堆 NSImages,我将它们保存在 PDFDocument 中,作为 PDFPages。当我第一次插入 NSImage 时,NSImage 的大小以点 (72 dpi) 为单位,而(仅)NSI
我试图理解为什么 var tmp: Any = NSImage(byReferencingFile: somePath) var img: NSImage = tmp as NSImage 不会工作并
好吧,我很困惑,因为这应该很简单。 在 Xcode 5 中,我开始了一个新的 iphone 项目。在 ViewDidLoad 中,我开始输入: NSImage *图像 ... 实际上在“NSI”点……
我正在尝试将文件从 xcassets 加载到 NSImage,其中 Assets 名称是 Logo 。 我试过这个: let logoIcon = NSImage(named: "logo") 但我一
我正在编写一个简单的 MacRuby 应用程序。我有一个 NSView,它在调用 drawRect() 时在自身内部绘制图像。 在那个 NSView 子类中,我的 drawRect() 有(以及其他一
所以我有一个 NSImage *startingImage 它由带有灰色色彩空间的 NSBitmapImageRep 表示 我需要反转它的颜色,因此我将其转换为 CIImage CIImage *
首先,我知道我不应该使用 SQLite 数据库来存储图像,但我只存储非常小的网站图标。 我的问题是我尝试将这些图标插入数据库(似乎有效),我将图标转换为 NSData与 -tiffrepresenta
我正在尝试制作一个简单的基于文档的 cocoa 应用程序,可以在 pdf 文件中保存和加载图像。 对于保存部分,我正在使用 - (NSData *)dataOfType:(NSString *)typ
我使用 NSAffineTransform 类在其中心旋转了 NSImage,它可以工作。代码在这里: - (NSImage*)rotateImage: (NSImage*)myImage angle
我是 cocoa 和编程的新手,如果这是基本的东西,我很抱歉。 我正在使用 ARC。我有一个 NSImageView 组件,它由 DropZone 类控制。我将图像拖放到其中,但是一旦我尝试调用我得到
我想创建 NSScrollView 对象的 NSImage,这样我就可以将平面图形用于动画目的。 当我将 ScrollView 对象渲染为图形并将其添加回我的窗口时,它可以工作,但看起来非常糟糕,就像
到目前为止,我已经构建了一个应用程序,允许用户将图像拖放到 NSImageView 上。但是,我希望能够通过简单地单击任何图像并按住鼠标按钮来移动其位置来移动这些图像。 设置图像后如何操作 NSIma
我正在尝试打印 NSImage,但无法将其调整为正确的尺寸。我希望 NSImage 在任何足够大的纸张上打印为 4"x6"照片。这是我现在的代码。 var printView = NSImageVie
这可能是一个新问题,但我无法理解如何操作 NSImage 实例。 我正在尝试创建一个方法,该方法将采用 NSImage (一个图标),在其上绘制另一个 NSImage (复选标记),然后返回修改后的
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
我正在尝试设置一个自定义拖动图标以在 NSTableView 中使用。一切似乎都正常,但由于我对 Quartz 缺乏经验,我遇到了问题。 - (NSImage *)dragImageForRowsWi
我有许多 NSImage,正在自定义 NSView 子类中渲染。当用户将鼠标放在这些图像上时,我希望放大这些图像,并且想知道执行此操作的最佳方法。 目前我正在使用 NSView 的 drawRect:
我的程序中有一个大型一维动态数组,它表示磁盘上的 FITS 图像,即它保存图像的所有像素值。数组的类型是double。目前,我只关心单色图像。 由于 Cocoa 不直接支持 FITS 格式,因此我使用
我想获取 NSImage.h 中定义的常量的图像文件,例如 NSImageNameGoRightTemplate 。我想复制并编辑其中一些。有谁知道这些图像位于哪里?我太笨了,在驱动器上找不到它们..
我是一名优秀的程序员,十分优秀!