3
我有一個iOS應用程序,它使用UIImage子類視圖和UIBezierPath繪製形狀的線在屏幕上繪製形狀。如何將UIImage視圖和文本保存到磁盤
這裏是繪製文本代碼:
// 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繪製的文本,還需要哪些代碼?