zoukankan      html  css  js  c++  java
  • iOS 学习

    绘制到位图

    下面利用位图图形上下文给一个图片添加水印,在下面的程序中我们首先创建上下文,然后在上下文中绘制图片、直线和文本,最后从当前位图上下文中取得最终形成的新图片显示到界面

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIImage *image = [self drawImageAtImageContext];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.center = CGPointMake(160, 282);
        [self.view addSubview:imageView];
        
    }
    //添加水印
    -(UIImage *)drawImageAtImageContext{
        //获得一个位图图形上下文
        CGSize size = CGSizeMake(300, 188);//画布大小
        
        UIGraphicsBeginImageContext(size);
        
        UIImage *image = [UIImage imageNamed:@"frame_shop_lovely_bg@2x.jpg"];
        //注意绘图的位置是相对于画布顶点而言,不是屏幕
        [image drawInRect:CGRectMake(0, 0, 300, 188)];
        //添加水印
        CGContextRef ref = UIGraphicsGetCurrentContext();
        //字符的长度和 font
        UIFont *font = [UIFont systemFontOfSize:15];
        NSString *str = @"大金毛";
        int strLength = str.length *font.pointSize;
        //两点确定一条直线
        //下划线的长度等于字符长度
        CGContextMoveToPoint(ref, 200, 178);
        CGContextAddLineToPoint(ref, strLength + 200, 178);
        //直线的颜色、宽度
        [[UIColor redColor]setStroke];
        CGContextSetLineWidth(ref, 2);
        //绘制图像到指定图形上下文,只有边框
        CGContextDrawPath(ref, kCGPathStroke);
        //字符的位置
        [str drawInRect:CGRectMake(200, 158, 100, 30) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor redColor]}];
        //返回绘制的新图形
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        //最后一定不要忘了关闭上下文
        UIGraphicsEndImageContext();
        
        return newImage;
    }

    来自KenshinCui

  • 相关阅读:
    新增html元素的使用
    音频的使用和插入以及动态文字的使用
    创建表单以及表单元素的使用
    今天学习image在html中的应用
    Repeater里查找控件
    CSS 基础教程
    自然图像的颜色统计特征 Image Color Statistics
    [转]UE中使用正则表达式的一些技巧
    [转]SQL Server 的事务和锁
    [转]Amtura 商务智能项目实现手记
  • 原文地址:https://www.cnblogs.com/asamu/p/5501520.html
Copyright © 2011-2022 走看看