亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在iOS Swift中為圖像添加文本?

如何在iOS Swift中為圖像添加文本?

一只名叫tom的貓 2019-08-16 16:30:14
如何在iOS Swift中為圖像添加文本?我已經環顧四周,并沒有成功地弄清楚如何拍攝文字,將其疊加在圖像上,然后將兩者合并為一個UIImage。我使用我能想到的搜索字詞已經讓Google筋疲力盡了,所以如果有人有解決方案或者至少提示他們可以指出它會非常感激。
查看完整描述

3 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

我的簡單方案:

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}


查看完整回答
反對 回復 2019-08-16
  • 3 回答
  • 0 關注
  • 1016 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號