八 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章