iOS 图片处理 生成文字图片

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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的方法

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