iOS 給圖片加水印

效果圖:

這裏寫圖片描述

核心代碼:

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //0.加載圖片
    UIImage *image = [UIImage imageNamed:@"阿狸頭像"];
    //1.開啓一個跟圖片原始大小的上下文
    //opaque:不透明度
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.把圖片繪製到上下文當中
    [image drawAtPoint:CGPointZero];
    //3.把文字繪製到上下文當中
    NSString *str = @"LayneCheung";

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    dict[NSBackgroundColorAttributeName] = [UIColor orangeColor];

    [str drawAtPoint:CGPointMake(60, 170) withAttributes:dict];
    //4.從上下文當中生成一張圖片.(把上下文當中繪製的所有內容,生成一張圖片)
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.關閉上下文.
    UIGraphicsEndImageContext();

    self.imageV.image = newImage;



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
發佈了55 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章