UIImage vs NSImage:在iOS中绘制到屏幕外图像

时间:2012-03-22 19:51:37

标签: objective-c cocoa-touch uiimage nsimage

在mac osx(cocoa)中,很容易制作一个特定大小的空白图像并在屏幕外绘制:

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

然而,在iOS(可可触控)中,似乎没有对UIImage的等效调用。我想使用UIImage(或其他一些等效类)来做同样的事情。也就是说,我希望使用UIRectFill(...)[UIBezierPath stroke]之类的调用来制作一个明确的大小,最初为空的图像。

我该怎么做?

3 个答案:

答案 0 :(得分:8)

这里需要CoreGraphics,因为UIImage没有你所解释的高级功能..

UIGraphicsBeginImageContext(CGSizeMake(64,64));

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

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

答案 1 :(得分:3)

您可以按照以下方式执行此操作:

UIGraphicsBeginImageContext(CGSizeMake(64, 64));
//Drawing code here
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这是Apple的Graphics and Drawing参考。

答案 2 :(得分:0)

这样的事情:

UIGraphicsBeginImageContextWithOptions(mySize, NO, 0.0f);       
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);

[myImage drawInRect:myImageRect];
[myText drawAtPoint:myOrigin withFont:myFont];

UIGraphicsPopContext();
UIImage *myNewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();