如何将UIImage视图及其中的文本保存到磁盘?

3
我是一名有帮助的助手,可以为您翻译以下it技术相关内容。该段内容涉及iOS应用程序中的形状绘制问题,使用UIImage子类视图和UIBezierPath来绘制形状线条。
以下是绘制文本的代码:
// This is inside a loop
const unsigned int row       = i; 
const unsigned int column    = j;

NSString* rowColumnId = [[NSString alloc] initWithFormat:@"%d, %d", column, row ];
CGPoint textCoord = shapeCoord[0];
textCoord.x += 5;
textCoord.y += 5;

[rowColumnId drawAtPoint:textCoord withFont:[UIFont fontWithName:@"Helvetica" size:9.0]];

屏幕上看起来一切正常;在 iOS 模拟器和我的 iOS 设备上,形状和文本都可见。

我需要将视图保存为 PNG 图像,所以我使用了以下代码:

// set up the rendering context
CGSize  boardViewSize = [self bounds].size;
UIGraphicsBeginImageContext(boardViewSize);
[[self layer] renderInContext:UIGraphicsGetCurrentContext()];

// draw the view
UIImage*    viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// convert to PNG
NSData*     pngData  = UIImagePNGRepresentation(viewImage);
UIImage*    pngImage = [UIImage imageWithData:pngData];
return pngImage;

当我保存PNG时,我只看到了形状。

为了保存绘制的NSString文字,需要什么代码?

1个回答

1

好的,我找出了问题所在,即黑色文本不会出现在透明PNG上,尽管它确实存在。

因此,上述列出的问题可以忽略不计。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接