iPhone绘图上下文
我并不是想在组件上绘图,我只是想创建一个新的上下文(我认为)并吐出一个 UIImage,其中包含我绘图的内容。我并不是想利用任何现有的组件。我正在使用以下代码:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, self.textSize.width, self.textSize.height, 8, 4 * self.textSize.width,
colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
NSString *text = @"Hello world";
[text drawAtPoint:CGPointMake(0, 0) forWidth:maxWidth withFont:font lineBreakMode:UILineBreakModeWordWrap];
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage * myRendering = [UIImage imageWithCGImage:imageMasked];
不幸的是,我收到“无效上下文”错误消息。搜索谷歌似乎只会让人们试图利用现有的组件。我想吐出一个新的UIImage。
我在这里尝试了创建位图上下文的示例 - http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid /TP30001066-CH203-SW9
我仍然得到:
: CGContextGetShouldSmoothFonts: 无效的上下文 : CGContextSetFont: 无效上下文 : CGContextSetTextMatrix: 无效上下文 : CGContextSetFontSize: 无效上下文 ETC...
I am not trying to draw on a component I am simply trying to create a new context (I think) and spit out a UIImage with the contents of my drawing on it. I'm not trying to draw on any existing component. I am using the following code:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, self.textSize.width, self.textSize.height, 8, 4 * self.textSize.width,
colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
NSString *text = @"Hello world";
[text drawAtPoint:CGPointMake(0, 0) forWidth:maxWidth withFont:font lineBreakMode:UILineBreakModeWordWrap];
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage * myRendering = [UIImage imageWithCGImage:imageMasked];
Unfortunately I am getting an "invalid context" error message. Searching Google only seemed to bring up people trying to draw on existing components. I want to spit out a new UIImage.
I tried the example here for creating a bitmap context - http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-SW9
I still get:
: CGContextGetShouldSmoothFonts: invalid context
: CGContextSetFont: invalid context
: CGContextSetTextMatrix: invalid context
: CGContextSetFontSize: invalid context
etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你总是根据上下文进行绘画。上下文本身可以链接到显示器、位图甚至 PDF。
根据您想要实现的目标,您必须创建一个上下文或使用现有的上下文。对于自定义组件(我假设您的意思是因为您提到了 JPanel),您只需像这样重写 UIView 的 drawRect 方法即可。
正如您所看到的,上下文已经为您创建了,您可以通过调用 UIGraphicsGetCurrentContext() 来获取它。
You always draw against a context. The context itself can be linked to the Display, a Bitmap or even a PDF.
Depending on what you want to achieve you have to create a context or use an existing one. For custom components (I assumed you mean this because you mentioned a JPanel) you just override the drawRect method of an UIView like so.
As you can see the context was already created for you and you get it by calling
UIGraphicsGetCurrentContext()
.首先,通过检查 CGBitmapContextCreate 的返回值来检查您是否实际创建了上下文。即
并检查控制台是否有任何消息。
如果您尚未创建上下文,请检查您是否在 iOS 4.0 或更高版本上进行开发。如果不是,请不要传递 NULL 作为第一个参数,而是传递
malloc
一块内存。First of all, check if you actually created a context by checking the return value of
CGBitmapContextCreate
. i.e.And check the console for any message.
If you haven't created a context, check if you are developing on iOS 4.0 or later. If not, don't pass NULL as first argument but
malloc
a chuck of memory.好吧,问题是我没有将位图绘制代码放入 viewDidLoad 中。我实在是看不懂这些东西。为什么这么不直观?
Ok so the problem was I didn't put my bitmap drawing code in viewDidLoad. I really don't understand this stuff. Why is it so unintuitive?