标签:
Main.storyboard
ViewController.m
//
// ViewController.m
// 5B04.图片水印
//
// Created by huan on 16/1/29.
// Copyright © 2016年 huanxi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 生成一张有水印的图片,一定要获取UIImage对象 然后显示在imageView上
//创建背景图片
UIImage *bgImage = [UIImage imageNamed:@"scene"];
//1.创建一个位图【图片】,开启位图上下文
//2.size:位图大小
//3.opaque:alpha 通道 YES:不透明/NO 透明 使用NO,生成的更清晰
//scale 比例 设置0.0为屏幕的比例 获取生成图片大小
//位图大小:20*20 生成一张:(20 *scale *20 *scale)
NSLog(@"当前屏幕的比例 %f", [UIScreen mainScreen].scale);
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
//2.画背景图
[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
//3.画水印
//算水印的位置和大小
CGFloat waterW = 80;
CGFloat waterH = 60;
CGFloat waterX = bgImage.size.width - waterW;
CGFloat waterY = bgImage.size.height - waterH;
UIImage *waterImage = [UIImage imageNamed:@"logo"];
[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
//4.从位图上下文获取 当前编辑的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.结束当前位置编辑
UIGraphicsEndImageContext();
//6.显示在UIImageView上
self.imageView.image = newImage;
}
@end
标签:
原文地址:http://www.cnblogs.com/Lu2015-10-03/p/5177724.html