如何在iOS Swift中為圖像添加文本?我已經(jīng)環(huán)顧四周,并沒有成功地弄清楚如何拍攝文字,將其疊加在圖像上,然后將兩者合并為一個(gè)UIImage。我使用我能想到的搜索字詞已經(jīng)讓Google筋疲力盡了,所以如果有人有解決方案或者至少提示他們可以指出它會(huì)非常感激。
3 回答

茅侃侃
TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊
我的簡(jiǎn)單方案:
func generateImageWithText(text: String) -> UIImage{ let image = UIImage(named: "imageWithoutText")! let imageView = UIImageView(image: image) imageView.backgroundColor = UIColor.clearColor() imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height) let label = UILabel(frame: CGRectMake(0, 0, image.size.width, image.size.height)) label.backgroundColor = UIColor.clearColor() label.textAlignment = .Center label.textColor = UIColor.whiteColor() label.text = text UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0); imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!) label.layer.renderInContext(UIGraphicsGetCurrentContext()!) let imageWithText = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return imageWithText}
- 3 回答
- 0 關(guān)注
- 1022 瀏覽
添加回答
舉報(bào)
0/150
提交
取消