#####图片水印 #####1.图片水印即在图片上下文上操作
- 第一:图片上下文不用在drawRect中写,在viewDidLoad中
- 第二:图片上下文需要自己手动创建
- 第三:往后的操作与drawRect的无什么差异
#####2.具体的看代码分析,将添加文字的那部分去掉,那么就只有图片了。
- (void)viewDidLoad {
[super viewDidLoad];
//加载图片
UIImage *image=[UIImage imageNamed:@"小黄人"];
//创建上下文,之后与drawRect一样的操作
//opaque:不透明度,不透明 Yes
//scale:取值为0不缩放
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//开启上下文画上图片
[image drawAtPoint:CGPointZero];
//再添加文字
NSString *str=@"HYL";
NSMutableDictionary *textDic=[NSMutableDictionary dictionary];
textDic[NSFontAttributeName]=[UIFont systemFontOfSize:30];
textDic[NSForegroundColorAttributeName]=[UIColor redColor];
textDic[NSStrokeWidthAttributeName]=@3;
//// UIColor, default nil: same as foreground color
textDic[NSStrokeColorAttributeName]=[UIColor blueColor];
NSShadow *shade=[[NSShadow alloc]init];
shade.shadowBlurRadius=1;
shade.shadowColor=[UIColor redColor];
shade.shadowOffset=CGSizeMake(2, 2);
textDic[NSShadowAttributeName]=shade;
//添加文字
[str drawAtPoint:CGPointMake(70, 340) withAttributes:textDic];
//从图片上下文里获得已经水印的图片
UIImage *image1=UIGraphicsGetImageFromCurrentImageContext();
//显示一下
self.imageView.image=image1;
}
#####3.效果图片
- 1.
#####4.源代码地址