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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值