美文网首页
iOS - 图片添加水印

iOS - 图片添加水印

作者: 骑行怪状 | 来源:发表于2016-09-13 22:58 被阅读92次
    春夏秋冬
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
        imageView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:imageView];
        
        
        /**
         *   UIGraphicsBeginImageContextWithOptions(<#CGSize size#>, <#BOOL opaque#>, <#CGFloat scale#>)
         *
         *   <#CGSize size#> : 位图的尺寸大小,这里大小设置为图片的尺寸大小
         *   <#BOOL opaque#> : opaque 为不透明度 , NO 为透明
         *   <#CGFloat scale#> : scale 缩放 取值为 0 ,表示不缩放
         *
         */
        
        UIImage *BGimage = [UIImage imageNamed:@"20141216155246_eifSM.jpg"];
       
        // 1、开启位图上下文,由于位图上下文跟 View 没有关系,所以不需要在 drawRect 中
        
        UIGraphicsBeginImageContextWithOptions(BGimage.size, NO, 0);
        
        // 2、绘制原生图片
        [BGimage drawAtPoint:CGPointZero];
        
        // 3、给原生图片添加文字
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[NSForegroundColorAttributeName] = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
        dic[NSFontAttributeName] = [UIFont systemFontOfSize:90];
        
        
        NSString *str = @"@春夏秋冬";
        [str drawAtPoint:CGPointMake(600, 2500) withAttributes:dic];
        
        
        // 4、生成一张图片,从上下文中获取当前的图片
        
        UIImage *imageWater = UIGraphicsGetImageFromCurrentImageContext();
        
        imageView.image = imageWater;
        
        // 关闭图形上下文
        UIGraphicsEndImageContext();
        
        
    }
    
    

    @来源 小码哥 视频, 感谢小码哥!

    相关文章

      网友评论

          本文标题:iOS - 图片添加水印

          本文链接:https://www.haomeiwen.com/subject/fifvettx.html