开发锦集iOS常用

ios 图片上添加文字

2019-05-05  本文已影响3人  YvanLiu
/**
 图片添加文字

 @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>

上一篇 下一篇

猜你喜欢

热点阅读