IOS图片上添加水印文字(微博上图片上得水印文字)


建一个类目(不知道怎么建的可以去百度一下) UIImage的类目

类目的.h文件  文件名为UIImage+watermark

#import <UIKit/UIKit.h>


@interface UIImage (watermark)

//定义一个方法

- (UIImage *)watermarkImage:(NSString *)text;

@end


.m文件的实现

#import "UIImage+watermark.h"


@implementation UIImage (watermark)

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


    //1.获取上下文

    UIGraphicsBeginImageContext(self.size);

    

    //2.绘制图片

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

    

    //3.绘制水印文字

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

    

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

    style.alignment = NSTextAlignmentCenter;

    //文字的属性

    NSDictionary *dic = @{

                          NSFontAttributeName:[UIFont systemFontOfSize:13],

                          NSParagraphStyleAttributeName:style,

                          NSForegroundColorAttributeName:[UIColor whiteColor]

                          };

    //将文字绘制上去

    [text drawInRect:rect withAttributes:dic];


    //4.获取绘制到得图片

    UIImage *watermarkImage = UIGraphicsGetImageFromCurrentImageContext();

    

    //5.结束图片的绘制

    UIGraphicsEndImageContext();

    

    return watermarkImage;

}

@end



然后再viewController 的视图上添加一个UIImageView 导入类目(UIImage+watermark)文件

#import "ViewController.h"

#import "UIImage+watermark.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    

    UIImage *image = [UIImage imageNamed:@"baymax.jpg"];


//调用类目的方法实现功能

    UIImage *img = [image watermarkImage:@"@大白"];

    

    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];

//    imageView.frame = self.view.bounds;

    

    [self.view addSubview:imageView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。