/**
圖片添加文字
@param image 圖片
@param text 文字
@param point 位置
@param attributed 文字樣式
@return 新圖片
*/
+ (UIImage *)imageSetString_image:(UIImage *)image
text:(NSString *)text
textPoint:(CGPoint)point
attributedString:(NSDictionary * )attributed;
+ (UIImage *)imageSetString_image:(UIImage *)image
text:(NSString *)text
textPoint:(CGPoint)point
attributedString:(NSDictionary * )attributed
{
//1.開啟上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2.繪制圖片
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//添加水印文字
[text drawAtPoint:point withAttributes:attributed];
//3.從上下文中獲取新圖片
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
//4.關閉圖形上下文
UIGraphicsEndImageContext();
//返回圖片
return img;
}
使用方法
image = [Tool imageSetString_image:image
text:@"test"
textPoint:CGPointMake(0, 0)
attributedString:@{NSForegroundColorAttributeName:[UIColor greenColor],
NSFontAttributeName:[UIFont systemFontOfSize:20]}];
Tool繼承自NSObject甥角,頭文件引入#import <UIKit/UIKit.h>
和#import <Foundation/Foundation.h>