八 iOS之 图片加水印

在一些app中常见到加水印的图片,为了防止别人盗图,下面看看怎么加上去的水印

如下图,在图片上绘制文字
这里写图片描述

新增一个UIImageView– “imageVCustom”
  • ViewController.m
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageVCustom;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self me];


}


-(void)me
{
    UIImage * image = [UIImage imageNamed:@"huangren"];

    //目前我们需要绘制图片到新的图片上,因此需要用到位图上下文

    //开启位图上下文
    //size: 位图上下文的尺寸
    //opaque: 不透明度  YES :不透明   NO:透明
    //scale:通常不需要缩放上下文,取值为0,表示不缩放
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

    //1、绘制原生的图片
    [image drawAtPoint:CGPointZero];

    //2、给原生的图片添加文字
    NSString * str = @"®鸽子";

    //添加字典属性
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor greenColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];

    [str drawAtPoint:CGPointMake(200, 528) withAttributes:dict];

    //3、生成一张图片给我们,从上下文中获取图片
    UIImage * imageNew = UIGraphicsGetImageFromCurrentImageContext();

    //4、关闭上下文
    UIGraphicsEndPDFContext();


    self.imageVCustom.image = imageNew;
}


@end

github demo : DrawWatermarkDemo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值