UIImage vs NSImage:在iOS中绘制为离屏图像 [英] UIImage vs NSImage: Drawing to an off screen image in iOS

查看:629
本文介绍了UIImage vs NSImage:在iOS中绘制为离屏图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mac osx(可可)中,很容易将特定大小的空白图片绘制到屏幕外:

  NSImage * image = [[NSImage alloc] initWithSize:NSMakeSize(64,64)]; 
[image lockFocus];
/ *这里绘制代码* /
[image unlockFocus];然而,在iOS(可可触摸)中,似乎没有对UIImage的等效调用。

我想使用UIImage(或一些其他等效类)做同样的事情。也就是说,我想使用 UIRectFill(...) [UIBezierPath]这样的调用来创建一个显式大小的初始空图像

解决方案



/ div>

CoreGraphics在这里需要,因为UIImage没有像你解释的高级功能。

  UIGraphicsBeginImageContext (CGSizeMake(64,64)); 

CGContextRef context = UIGraphicsGetCurrentContext();
//在这里绘制代码(使用上下文)

UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


In mac osx (cocoa), It is very easy to make a blank image of a specific size and draw to it off screen:

NSImage* image = [[NSImage alloc] initWithSize:NSMakeSize(64,64)];
[image lockFocus];
/* drawing code here */
[image unlockFocus];

However, in iOS (cocoa touch) there does not seem to be equivalent calls for UIImage. I want to use UIImage (or some other equivalent class) to do the same thing. That is, I want to make an explicitly size, initially empty image to which I can draw using calls like UIRectFill(...) and [UIBezierPath stroke].

How would I do this?

解决方案

CoreGraphics is needed here, as UIImage does not have high level functions like what you explained..

UIGraphicsBeginImageContext(CGSizeMake(64,64));

CGContextRef context = UIGraphicsGetCurrentContext();
// drawing code here (using context)

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这篇关于UIImage vs NSImage:在iOS中绘制为离屏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆