我建议在代码中生成图像,因为快照屏幕会限制您的屏幕大小。此外,我发现当一切都在代码中时,管理复杂的操作会更容易。
CGContextDrawImage将帮助您将原始图像复制到新图像的区域。
下面是一个如何自己创建包含居中文本和形状的图像的示例(在这种情况下,三角形上面带有矩形框,名称居中)。
// Get font from custom class and create text styles
UIFont *font = [CBFontHelper robotoMedium:32.0f];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *textAttributes = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName: [CBColourHelper white],
};
CGSize textSize = [name sizeWithAttributes:textAttributes];
// various bits of paddings and heights neccessary to calculate total image size
float gap = 2.0f;
float textHeight = 0.3f*interfacePadding + textSize.height + 0.3f*interfacePadding;
float width = 200;
float height = textHeight + gap + 2*interfacePadding;
//create new image context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), false, 0.0f);
// Fill rectangle to hold name
[[CBColourHelper sandstone] setFill];
UIRectFill(CGRectMake(0.0f,0.0f,width,textHeight));
//Draw name over rectangle
[self drawString:name withFont:font inRect:CGRectMake(0.5f*interfacePadding, 0.3f*interfacePadding, width, height)];
// Draw triangle
[[CBColourHelper sandstone] setFill];
UIBezierPath *triangle = [UIBezierPath bezierPath];
[triangle moveToPoint:CGPointMake(width/2,textHeight + gap)];
[triangle addLineToPoint:CGPointMake(width/2,height)];
[triangle addLineToPoint:CGPointMake(width/2+5.0f*interfacePadding,textHeight + gap)];
[triangle closePath];
[triangle fill];
// Get image from context
UIImage *markerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();