iOS图片水印实现

图片水印
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.源代码地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值