objective-c - UIImage 与 NSImage : Drawing to an off screen image in iOS

标签 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 (cocoa touch) 中似乎没有对 UIImage 的等效调用。我想使用 UIImage(或其他一些等效类)来做同样的事情。也就是说,我想制作一个明确大小的、最初为空的图像,我可以使用像 UIRectFill(...) 这样的调用来绘制它。和 [UIBezierPath stroke] .

我该怎么做?

最佳答案

这里需要 CoreGraphics,因为 UIImage 没有像您解释的那样的高级功能..

UIGraphicsBeginImageContext(CGSizeMake(64,64));

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

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

关于objective-c - UIImage 与 NSImage : Drawing to an off screen image in iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9829475/

相关文章:

iphone - 将样本音频44100Kz下调至8000Kz

iphone - 如何使用自定义按钮关闭操作表

ios - 带图像的水平滚动 uicollectionView

iphone - 使用mapkit计算两个地址之间的行驶距离?

iphone - 刷新 MKMapView 上的 MKAnnotationView

ios - 如何在 iOS 中播放 .m3u8 或 .ts 文件?

ios - Swift:更改标签栏的图像色调颜色?

ios - 在 iPhone 应用程序中保存图片

ios - 无法找到 ScrollView 属性以了解 ScrollView 滚动了多少像素

ios UIImageJPEGRepresentation() 大图像崩溃