- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建内存中图像,在其上绘制并保存到磁盘。
当前代码是:
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:256
pixelsHigh:256
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:YES
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:8
];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:rep]];
// Draw your content...
NSRect aRect=NSMakeRect(10.0,10.0,30.0,30.0);
NSBezierPath *thePath=[NSBezierPath bezierPathWithRect:aRect];
[[NSColor redColor] set];
[thePath fill];
[NSGraphicsContext restoreGraphicsState];
NSData *data = [rep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"test.png" atomically: NO];
尝试在当前上下文中绘制时,出现错误
CGContextSetFillColorWithColor: invalid context 0x0
这里出了什么问题?为什么 NSBitmapImageRep 返回的上下文为 NULL?创建绘制图像并保存它的最佳方法是什么?
更新:
最终得出以下解决方案:
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(256, 256)];
[image lockFocus];
NSRect aRect=NSMakeRect(10.0,10.0,30.0,30.0);
NSBezierPath *thePath=[NSBezierPath bezierPathWithRect:aRect];
[[NSColor redColor] set];
[thePath fill];
[image unlockFocus];
NSData *data = [image TIFFRepresentation];
[data writeToFile: @"test.png" atomically: NO];
最佳答案
您的解决方法对于当前的任务是有效的,但是,它是比实际让 NSBitmapImageRep 工作更昂贵的操作!请参阅http://cocoadev.com/wiki/NSBitmapImageRep进行一些讨论..
请注意,[NSGraphicsContext graphicsContextWithBitmapImageRep:] 文档说:
"This method accepts only single plane NSBitmapImageRep instances."
您正在使用 isPlanar:YES 设置您的 NSBitmapImageRep,因此它使用多个平面...将其设置为 NO - 您应该可以开始了!
换句话说:
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:256
pixelsHigh:256
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0
];
// etc...
关于macos - 在 NSBitmapImageRep 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799920/
我有一个包含 JPEG 图像数据的缓冲区。我需要在 UIImageView 中显示此图像。我需要将此图像缓冲区转换为 UIImage 对象并按如下方式使用它 NSData *data = [NSDat
我有一个 char* 数据数组,它是 RGBA 格式,然后移至 ARGB 底线是设置的应用程序图像看起来完全困惑,我无法指出为什么? //create a bitmap representat
有没有办法在具有子像素抗锯齿功能的 NSBitMapImageRep 中绘制文本?文本绘制在不透明的白色背景上,我尝试使用 CGContextSetShouldSmoothFonts 函数,但这似乎没
首先:我想使用NSBezierPath在我的应用程序中绘制一些简单的按钮图稿,所以我想我应该创建一个 NSBitmapImageRep ,得到CGImage ,创建一个NSImage然后在按钮上调用
我尝试构建图像。逐个像素。所以首先我构建了一些类,它可以在每个像素上循环绘制不同的颜色。但它仅在将 alpha 设置为 255 时才有效。更改 alpha 会使颜色变暗并改变图片。大小和位置都可以。
我正在尝试将 NSImage 读入 NSBitmapImageRep,以更改某些像素的颜色。程序应该读取每个像素的颜色值,并检查它是否等于颜色池选择的颜色。我有一个名为“MainImageView”的
所以...我将图像加载到 NSBitmapImageRep 对象中,因此我能够通过二维数组检查特定像素的内容。现在我想对图像应用一些“转换”,为一些额外的处理做准备。如果我在 Photoshop 中手
我想使用 NSBitmapImageRep 在代码中构造一个 64x64 像素的 Sprite ,然后将其绘制到屏幕上,放大得很大。结果将是屏幕上出现非常大的“像素”。想想老派的《马里奥兄弟》或《我的
我有一种方法可以分析从 CGImageRef 构建的 NSBitmapImageRep 内的像素数据。相关代码如下: CGImageRef ref; // omitted code for initi
我正在开发我的第一个 10.5+ 版本的 mac osx cocoa 应用程序,其中我有一个 CVImageBufferRef (使用 QTKit 捕获),我需要通过 TCP 套接字将此图像传输到客户
获取 NSBitmapImageRep 的大小(宽度和高度)的最佳方法是什么? 这就是我现在正在做的事情: NSBitmapImageRep *bitmap; NSInteger samplesPer
我正在尝试创建内存中图像,在其上绘制并保存到磁盘。 当前代码是: NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
我有一个像这样创建的NSBitmapImageRep: NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
我想了解在构建 NSBitmapImageRep 时如何计算“bytesPerRow”(在我的例子中是从将 float 组映射到灰度位图)。 澄清这个细节将帮助我理解内存如何从 float 组映射到字
如何在 NSImageView 中绘制 NSBitmapImageRep? 最佳答案 NSImage *im = [[[NSImage alloc] init] autorelease]; [im a
我在 C++ 应用程序中使用 Objective-C 在 OSX 上加载图像。它是一个 OpenGL 应用程序。这是我用来将图像加载到 opengl 中的代码: bool OSXSpecifics::
我有一个用 xcode 6.1 创建的 osx xcode 项目我想用它来训练一下 SWIFT 的使用。 在我的一个观点中,我尝试创建一个 NSBitMapImageRep,如下所示: class B
如何将 NSImage 转换为 NSBitmapImageRep?我有代码: - (NSBitmapImageRep *)bitmapImageRepresentation { NSBitma
我正在尝试编写一个原型(prototype)来证明从一种格式到另一种格式的 RAW 转换是可能的。我必须将尼康的 .NEF 格式的原始文件转换为佳能的 .CR2 格式。在各种帖子的帮助下,我创建了原始
我正在尝试创建一个每个样本 32 位和一个 Alpha channel (每像素总共 128 位)的 NSBitmapImageRep 对象。 我的代码如下所示: let renderSize = N
我是一名优秀的程序员,十分优秀!