为图片添加文字

来源:互联网 发布:数据透视图显示百分比 编辑:程序博客网 时间:2024/05/17 04:58

#import "UIImage+watermark.h"


#define RGB_COLOR(R,G,B)[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]


@implementation UIImage (watermark)


- (UIImage *)watermarkImageWithText:(NSString *)text{

    UIGraphicsBeginImageContext(self.size);//获取上下文


    //绘制图片

    [self drawInRect:CGRectMake(0,0, self.size.width,self.size.height)];


    CGRect rect = CGRectMake(0,self.size.height-20, self.size.width,20);


    //段落格式

    NSMutableParagraphStyle  * style =[[NSMutableParagraphStyledefaultParagraphStyle]mutableCopy];


    style.alignment = NSTextAlignmentCenter;


    int R = arc4random()%255;

    int G = arc4random()%255;

    int B = arc4random()%255;


    //文字属性

    NSDictionary *dic =@{

                          NSFontAttributeName:[UIFontboldSystemFontOfSize:15],

                          NSParagraphStyleAttributeName:style,

                          NSForegroundColorAttributeName:RGB_COLOR(R,G,B)};



    //将文字绘制上去

    [text drawInRect:rectwithAttributes:dic];


    //获取绘制到的图片

    UIImage *watermarkImage =UIGraphicsGetImageFromCurrentImageContext();


    //结束图片绘制

    UIGraphicsEndImageContext();


    return watermarkImage;



}


0 0
原创粉丝点击