简体   繁体   English

将文本添加到UIImage SWIFT IOS 11

[英]Adding Text to a UIImage SWIFT IOS 11

I have an image saved in a UIImageView.image variable name ImageView. 我将图像保存在UIImageView.image变量名称ImageView中。 I'm trying to get text simply added to the ImageView.image so it simply shows the picture with the words "Hello". 我试图将文本简单地添加到ImageView.image中,以便仅显示带有“ Hello”字样的图片。

I've tried the following and NONE of them work. 我已经尝试了以下方法,但没有一个起作用。 I just get the same exact image back. 我刚得到相同的图像。 I've been at this for days now and I have gotten very frustrated with the non working answers on Stack Overflow: Code I've tried: 我已经待了好几天了,我对堆栈溢出的无效答案感到非常沮丧:我尝试过的代码:

    ImageView.Image = textToImage(drawText: "Hello", inImage: ImageView.image!, atPoint: CGPoint(x: 20, y: 20))
    ImageView.image = generateImageWithText(text: "Hello")

func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
    let textColor = UIColor.white
    let textFont = UIFont(name: "Marker Felt", size: 12)!
    let scale = image.scale
        //UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale)

    let textFontAttributes = [
        NSAttributedStringKey.font: textFont,
        NSAttributedStringKey.foregroundColor: textColor,
        ] as [NSAttributedStringKey : Any]
    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))

    let rect = CGRect(origin: point, size: image.size)
    text.draw(in: rect, withAttributes: textFontAttributes)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    //CameraImageView.image = newImage
    UIGraphicsEndImageContext()

    return newImage!
}

func generateImageWithText(text: String) -> UIImage
{
    let image = CameraImageView.image

    let imageView = UIImageView(image: image)
    imageView.backgroundColor = UIColor.clear
    imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: (image?.size)!)

    let label = UILabel(frame: CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 3024.0, height: 4032.0)/*(width://image?.size)!*/))
    label.backgroundColor = UIColor.clear
    label.textAlignment = .center
    label.textColor = UIColor.white
    label.text = text

    UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)
    imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
    label.layer.render(in: UIGraphicsGetCurrentContext()!)
    let imageWithText = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();

    return imageWithText!
}

Here's a demo: 这是一个演示:

在此处输入图片说明

Here's the complete code: 这是完整的代码:

class ViewController: UIViewController {
    @IBOutlet weak var iv: UIImageView!
    @IBOutlet weak var tf: UITextField!

    @IBAction func doGo(_ sender: Any) {
        let s = tf.text!
        let s2 = NSAttributedString(string: s, attributes: 
            [.font:UIFont(name: "Georgia", size: 100)!, 
             .foregroundColor: UIColor.yellow])
        let sz = iv.image!.size
        let r = UIGraphicsImageRenderer(size:sz)
        iv.image = r.image {
            _ in
            iv.image!.draw(at:.zero)
            s2.draw(at: CGPoint(x:30, y:sz.height-150))
        }
    }
    @IBAction func doRevert(_ sender: Any) {
        config()
    }
    func config() {
        iv.image = UIImage(named:"nice.jpg")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        config()
    }
}

Go ye and do likewise. 去吧,做同样的事情。

// adding text to image //在图片上添加文字

    let titleLabel = UILabel(frame: CGRect(x: 10, y: 0, width: imageView.frame.width - 10, height: 30))
    titleLabel.text = "userName"
    titleLabel.textColor = UIColor.black
    titleLabel.font = UIFont(name:"chalkboard SE", size: 18)
    imageView.addSubview(titleLabel)

Ok, I figured it out. 好的,我知道了。 It has to do with the UIImageView field being dynamic. 它与UIImageView字段是动态的有关。 I capture an image from the camera and saved it into the CameraImageView.image, which is then saved into another UIImage variable. 我从相机捕获图像并将其保存到CameraImageView.image中,然后将其保存到另一个UIImage变量中。 You can NOT add text to this type of image as the app doesn't know what kind of image it is (ie .jpg/.PNG, etc). 您无法向此类图像添加文本,因为应用程序不知道它是哪种图像(即.jpg / .PNG等)。

If the image is saved as a HARD image in the app (in the assets folder), then you can add text to the image. 如果该图像在应用程序(在Assets文件夹中)另存为HARD图像,则可以向该图像添加文本。 Since no image is saved in the assets folder the app simply does nothing as it doesn't know what to do. 由于资产文件夹中未保存任何图像,因此该应用程序根本不执行任何操作,因为它不知道该怎么办。

I created a blank/clear png file and saved it in the assets folder. 我创建了一个空白/清除png文件,并将其保存在资产文件夹中。 I then combined that saved image with the image from the CameraImage into another UIImage variable and displayed that. 然后,我将保存的图像与CameraImage中的图像合并到另一个UIImage变量中并显示出来。

Here's the Code: 这是代码:

    let bottomImage = CameraImageView.image
    var topImage = UIImage(named: "HelloWorld.png")
    let size = CGSize(width: 242, height: 243)

    UIGraphicsBeginImageContext(size)
    let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    bottomImage!.draw(in: areaSize)
    topImage!.draw(in: areaSize, blendMode: CGBlendMode(rawValue: 0)!, alpha: 0.8)

    //the constant below is what takes the 2 images from the context and merges them into 1 new image
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!

    //set the combined image into the empty Imageview variable, and then display it
    ImageViewEmpty.image = newImage
    UIGraphicsEndImageContext()
    ImageViewEmpty.isHidden = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM