专栏首页码客iOS 图片处理 生成文字图片

iOS 图片处理 生成文字图片

工具类

import UIKit

///图片工具类
class ZJImageUtils{
    static var textBgColor:[String:UIColor] = [:];
    
    internal static func randomColor()-> UIColor{
        var color = ["#E1B154","#D2945B",
                     "#E57257","#38B1A2",
                     "#76A174","#5CA7C7","#B758A9",
                     "#F99A5A"
        ];
        let index = Int(arc4random_uniform(UInt32(color.count)));
        let hex = color[index];
        return UIColor(hexString: hex, alpha: 1.0)!;
    }
    
    ///文子转图片
    static func imageFromText(_ bgColor:UIColor,str:String,imageWidth:CGFloat)->UIImage{
        
        let size = CGSize(width: imageWidth, height: imageWidth);
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
        let context:CGContext =  UIGraphicsGetCurrentContext()!;//获取画笔上下文
        
        context.setAllowsAntialiasing(true) //抗锯齿设置
        
        bgColor.set();
        
        UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height));
        
        let fontWidth = imageWidth/1.4/2;
        
        
        let y = (imageWidth - fontWidth*1.3)/2;
        //画字符串
        let font = UIFont.systemFont(ofSize: fontWidth);
        
        let attrs = [NSAttributedStringKey.font:font,NSAttributedStringKey.foregroundColor:UIColor.white];
        
        if(str.count>=2){
            let subStr:NSString = String(str.suffix(2)) as NSString;
            let x = (imageWidth - subStr.size(withAttributes: attrs).width)/2;
            subStr.draw(at: CGPoint(x: x, y: y), withAttributes:attrs);
        }else if(str.count==1){
            let x = (imageWidth - str.size(withAttributes: attrs).width)/2;
            str.draw(at: CGPoint(x: x, y: y), withAttributes:attrs);
        }else{
            
        }
        
        // 转成图片
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image!;
        
    }
    
    static func imageFromTextRandomBg(str:String,imageWidth:CGFloat)->UIImage{
        var bgColor:UIColor = randomColor();
        //设置背景色
        if(textBgColor[str] != nil){
            bgColor = textBgColor[str as String]!;
        }else{
            textBgColor[str] = bgColor;
        }
        return imageFromText(bgColor, str: str, imageWidth: imageWidth);
    }
    
    ///指定大小缩放
    static func imageZoomBySize(_ sourceImage:UIImage,newSize:CGSize)->UIImage{
        UIGraphicsBeginImageContext(newSize);
        sourceImage.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height));
        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage!;
    }
    
    ///等比例缩放,最大宽度,小图片不放大
    static func imageZoomByWidth(_ sourceImage:UIImage,maxWidth:CGFloat) -> UIImage{
        let imageSize = sourceImage.size;
        let width = imageSize.width;
        let height = imageSize.height;
        let targetWidth = (width >= maxWidth ? maxWidth : width);
        let targetHeight = (targetWidth / width) * height;
        if(targetWidth==width){
            return sourceImage;
        }else{
            return imageZoomBySize(sourceImage, newSize: CGSize(width: targetWidth, height: targetHeight))
        }
        
    }
    
    ///等比例缩放,最大高度,小图片不放大
    static func imageZoomByHeight(_ sourceImage:UIImage,maxHeight:CGFloat) -> UIImage{
        let imageSize = sourceImage.size;
        let width = imageSize.width;
        let height = imageSize.height;
        let targetHeight = (height >= maxHeight ? maxHeight : height);
        let targetWidth = (targetHeight / height) * width;
        if(targetHeight==height){
            return sourceImage;
        }else{
            return imageZoomBySize(sourceImage, newSize: CGSize(width: targetWidth, height: targetHeight))
        }
    }
    
    ///等比例缩放,最大高度,最大宽度,小图片不放大
    static func imageZoomByWidthHeight(_ sourceImage:UIImage,maxWidth:CGFloat,maxHeight:CGFloat) -> UIImage{
        let imageSize = sourceImage.size;
        let width = imageSize.width;
        let height = imageSize.height;
        
        if(width < maxWidth && height < maxHeight){
            return sourceImage;
        }else{
            let widthRatio = width / maxWidth;
            let heightRatio = height / maxHeight;
            let maxRatio = widthRatio > heightRatio ? widthRatio : heightRatio;
            let targetHeight = height / maxRatio;
            let targetWidth = width / maxRatio;
            
            return imageZoomBySize(sourceImage, newSize: CGSize(width: targetWidth, height: targetHeight))
        }
    }
    
    ///压缩JPG
    static func imageCompressJPG(_ sourceImage:UIImage)->Data{
        return UIImageJPEGRepresentation(sourceImage, 0.7)!;
    }
    
    ///压缩PNG
    static func imageCompressPng(_ sourceImage:UIImage)->Data{
        return UIImagePNGRepresentation(sourceImage)!;
    }
    
    ///图片模糊处理
    static func mohu(_ sourceImage:UIImage) -> UIImage{
        let context:CIContext = CIContext(options: nil);
        let inputImage = CIImage(image: sourceImage);
        
        // create gaussian blur filter
        
        let filter = CIFilter(name: "CIGaussianBlur")!;
        filter.setValue(inputImage, forKey: kCIInputImageKey);
        filter.setValue(NSNumber(value: 1.0 as Float), forKey: "inputRadius");
        
        // blur image
        
        let result:CIImage = filter.value(forKey: kCIOutputImageKey) as! CIImage;
        
        let cgImage:CGImage = context.createCGImage(result, from: result.extent)!;
        let image = UIImage(cgImage: cgImage);
        return image;
    }
    
    ///保存image为jpg文件
    static func saveJpg(_ sourceImage:UIImage) -> (Bool,String){
        let newImage = imageZoomByWidthHeight(sourceImage, maxWidth: 800, maxHeight: 800);
        let uuidStr = ZJStringUtils.getUUID();
        let documentsPath: AnyObject = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as AnyObject
        let jpgPath = documentsPath.appending("/\(uuidStr).jpg");
        print(sourceImage)
        let result = (try? UIImageJPEGRepresentation(newImage, 0.7)!.write(to: URL(fileURLWithPath: jpgPath), options: [.atomic])) != nil;
        if(result){
            return (true,jpgPath);
        }else{
            return (false,jpgPath);
        }
    }
}

其中涉及的获取UUID的方法

///获取没有-的uuid字符串
static func getUUID() -> String{
   let uuidStr = UUID().uuidString;
   let uuidNewStr = replace(uuidStr, replaceStr: "-", withStr: "");
   return uuidNewStr;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • iOS文件操作

    剑行者
  • Swift4 String的用法

    剑行者
  • iOS常用代码段

    控制器中代码设置 > storybord设置 > 全局设置 优先级高的会覆盖优先级低的配置,比如storybord中的设置了navigationbar的样式 那...

    剑行者
  • ES6语法基础之let用法

    简单讲解一些ES6语法基础!了解一些es6新特性!当然下一步需要学习的vue框架也是基于es6的,因此很有必要学习下es6语法,接下来几次简单讲解es6语法!

    十月梦想
  • 杂记rust的destructuring binding(反结构化绑定)与ownership(所有权)

    看rust by example看得我想睡觉...突然遇到个关于反结构化绑定的奇怪的特性:

    racaljk
  • Swift基础---Integers

    用户3004328
  • swift基础1

    用户2554571
  • ES6

    ES的全称是ECMAScript,它是由ECMA国际标准化组织制定的一项脚本语言的标准化规范。

    eadela
  • 变量的解构赋值

    上面代码中,函数move的参数是一个对象,通过对这个对象进行解构,得到变量x和y的值。如果解构失败,x和y等于默认值。

    达达前端
  • ES2018

    但如果数据源是异步的,for...of循环就只能拿到一堆Promise,而不是想要的值:

    ayqy贾杰

扫码关注云+社区

领取腾讯云代金券