UIGraphics 向图片上写字

相关的size自己设置一下就可以了

- ( UIImage *)createImageWithString:( NSString *)str
{
   
CGSize textSize = [ NSString sizeWithText :str font :[ UIFont systemFontOfSize : 12.0 ] maxSize : CGSizeMake ( MAXFLOAT , 11 )];
   
   
UIImage * image = [ UIImage imageNamed : @"ic_kehu_name" ];
   
CGSize contextSize = CGSizeMake (textSize. width , 12 + 15 );
   
   
// 开始图形上下文
   
UIGraphicsBeginImageContextWithOptions (contextSize, NO , 0.0 );
   
   
// 画图
    [image
drawAtPoint : CGPointMake ((textSize. width / 2 ) - 6 , 15 )];
   
   
// 获得一个位图图形上下文
   
CGContextRef context = UIGraphicsGetCurrentContext ();
   
   
CGContextDrawPath (context, kCGPathStroke );
   
    [str
drawAtPoint : CGPointMake ( 0 , 0 ) withAttributes : @{ NSFontAttributeName : [ UIFont systemFontOfSize : 12.0 ], NSForegroundColorAttributeName :[ UIColor blackColor ] } ];
   
   
// 返回绘制的新图形
   
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
   
   
UIGraphicsEndImageContext ();
   
   
return newImage;
}

你可能感兴趣的:(UIGraphics,向图片上写字)