zoukankan      html  css  js  c++  java
  • 添加图片水印和裁剪图片

    1.添加水印图片

    UIImage *bgImage = [UIImage imageNamed:@"scene"];
        // 上小文 : 基于位图(bitmap) ,  所有的东西需要绘制到一张新的图片上去
        // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)
        // size : 新图片的尺寸
        // opaque : YES : 不透明,  NO : 透明
        // 这行代码过后.就相当于常见一张新的bitmap,也就是新的UIImage对象
        UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);  
        // 2.画背景
        [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
        // 3.画右下角的水印
        UIImage *waterImage = [UIImage imageNamed:@"logo"];
        CGFloat scale = 0.2;
        CGFloat margin = 5;
        CGFloat waterW = waterImage.size.width * scale;
        CGFloat waterH = waterImage.size.height * scale;
        CGFloat waterX = bgImage.size.width - waterW - margin;
        CGFloat waterY = bgImage.size.height - waterH - margin;
        [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
        
        // 4.从上下文中取得制作完毕的UIImage对象
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();   
        // 5.结束上下文
        UIGraphicsEndImageContext();  
        // 6.显示到UIImageView
        self.iconView.image = newImage;
        
        // 7.将image对象压缩为PNG格式的二进制数据
        NSData *data = UIImagePNGRepresentation(newImage);
        //    UIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)
        
        // 8.写入文件
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
        [data writeToFile:path atomically:YES];

    2.裁剪图片(加圆环的)

    // 1.加载原图
        UIImage *oldImage = [UIImage imageNamed:@"me"];
        
        // 2.开启上下文
        CGFloat borderW = 2; // 圆环的宽度
        CGFloat imageW = oldImage.size.width + 2 * borderW;
        CGFloat imageH = oldImage.size.height + 2 * borderW;
        CGSize imageSize = CGSizeMake(imageW, imageH);
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
        
        // 3.取得当前的上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        // 4.画边框(大圆)
        [[UIColor whiteColor] set];
        CGFloat bigRadius = imageW * 0.5; // 大圆半径
        CGFloat centerX = bigRadius; // 圆心
        CGFloat centerY = bigRadius;
        CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
        CGContextFillPath(ctx); // 画圆
        
        // 5.小圆
        CGFloat smallRadius = bigRadius - borderW;
        CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
        // 裁剪(后面画的东西才会受裁剪的影响)
        CGContextClip(ctx);
        
        // 6.画图
        [oldImage drawInRect:CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height)];
        
        // 7.取图
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        // 8.结束上下文
        UIGraphicsEndImageContext();
        
        // 9.显示图片
        self.iconView.image = newImage;
        
        // 10.写出文件
        NSData *data = UIImagePNGRepresentation(newImage);
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
        [data writeToFile:path atomically:YES];

  • 相关阅读:
    韦德螺旋: 这真是一个螺旋吗?
    山上你能看到什么动物?
    你能够30秒内一字不差的念完它吗? 注意, 是读“颜色”, 不是让你识字.
    路透斯沃德的不可能的三角形
    换个角度, 青蛙也许就是白马王子
    这是一张很有趣的图片, 通常女性会先看到月亮, 男性会先看到人脸. 如果相反, 表示你体内的异性荷尔蒙偏高哦!
    亲吻的情侣幻觉: 这幅虚幻的亲吻由美国艺术家杰里•唐恩创作.
    PostgreSQL的 initdb 源代码分析之七
    PostgreSQL的initdb 源代码分析之六
    PostgreSQL的initdb 源代码分析之五
  • 原文地址:https://www.cnblogs.com/qingsongeasy/p/3667563.html
Copyright © 2011-2022 走看看