- (UIImage *)createShareImage:(NSString *)imageName Context:(NSString *)text { UIImage *sourceImage = [UIImage imageNamed:imageName]; CGSize imageSize; //画的背景 大小 imageSize = [sourceImage size]; UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); [sourceImage drawAtPoint:CGPointMake(0, 0)]; //获得 图形上下文 CGContextRef context=UIGraphicsGetCurrentContext(); CGContextDrawPath(context, kCGPathStroke); CGFloat nameFont = 20.f; //画 自己想要画的内容 NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]}; CGRect sizeToFit = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, nameFont) options:NSStringDrawingUsesDeviceMetrics attributes:attributes context:nil]; NSLog(@"图片: %f %f",imageSize.width,imageSize.height); NSLog(@"sizeToFit: %f %f",sizeToFit.size.width,sizeToFit.size.height); CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); [text drawAtPoint:CGPointMake((imageSize.width-sizeToFit.size.width)/2,0) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]}]; //返回绘制的新图形 UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }