Ios 如何在图片的不同位置添加多个文本?

Ios 如何在图片的不同位置添加多个文本?,ios,swift,xcode,Ios,Swift,Xcode,我目前正在制作一个摄像头应用程序,我可以从图片上的文本字段添加单个文本 我想要的是,我想把多个不同的文本放到图片上。所以我在故事板中添加了4个文本字段,现在我需要在视图控制器上更改一些内容。我尝试了一些东西,但解决不了 我尝试过使用此代码,但它不起作用 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

我目前正在制作一个摄像头应用程序,我可以从图片上的文本字段添加单个文本

我想要的是,我想把多个不同的文本放到图片上。所以我在故事板中添加了4个文本字段,现在我需要在视图控制器上更改一些内容。我尝试了一些东西,但解决不了

我尝试过使用此代码,但它不起作用

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage(CustomerTextBox.text!, inImage: image, atPoint: CGPoint( x: 400, y: 100)
        ImageDisplay.image = textToImage(ResponsibleTextBox.text!, inImage: image, atPoint: CGPoint( x: 400, y: 200)
        ImageDisplay.image = textToImage(LocationTextBox.text!, inImage: image, atPoint: CGPoint( x: 400, y: 300)
        ImageDisplay.image = textToImage(DescriptionTextBox.text!, inImage: image, atPoint: CGPoint( x: 400, y: 300)
        }
    dismissViewControllerAnimated(true, completion: nil)
这是我的屏幕:

这是我使用单个文本框的工作代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var PhotoLibrary: UIButton!
@IBOutlet weak var Camera: UIButton!
@IBOutlet weak var ImageDisplay: UIImageView!

@IBOutlet weak var CustomerTextBox: UITextField!
@IBOutlet weak var ResponsibleTextBox: UITextField!
@IBOutlet weak var LocationTextBox: UITextField!
@IBOutlet weak var DescriptionTextBox: UITextField!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func PhotoLibraryAction(sender: UIButton) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .PhotoLibrary

    presentViewController(picker, animated: true, completion: nil)
}

@IBAction func CameraAction(sender: UIButton) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera

    presentViewController(picker, animated: true, completion: nil)
}



func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage(CustomerTextBox.text!, inImage: image, atPoint: CGPoint( x: 400, y: 100))
    }

    dismissViewControllerAnimated(true, completion: nil)

}


func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

    // Setup the font specific variables
    let textColor: UIColor = UIColor.blackColor()
    let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 200)!

    //Setup the image context using the passed image.
    UIGraphicsBeginImageContext(inImage.size)

    //Setups up the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ]

    //Put the image into a rectangle as large as the original image.
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    // Creating a point within the space that is as bit as the image.
    let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    //Now Draw the text into an image.
    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    // Create a new image out of the images we have created
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    // End the context now that we have the image we need
    UIGraphicsEndImageContext()

    //And pass it back up to the caller.
    return newImage
}
}

在代码中,您将逐个添加文本并返回完整图像。您遵循的过程与在文本所需的空间中绘制文本不同,它将文本写入图像并返回完整图像

因此,修改您的方法以一次传递所有文本,并在图像上绘制所有文本并返回完整图像

我在这里向您展示我获取静态点的代码,您可以根据您的要求放置

以下是修改后的代码:

func textToImage(drawText: NSString, drawText1: NSString, drawText2: NSString, drawText3: NSString, inImage: UIImage, atPoint:CGPoint, atPoint1:CGPoint, atPoint2:CGPoint, atPoint3:CGPoint)->UIImage{

    // Setup the font specific variables
    let textColor: UIColor = UIColor.redColor();
    let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 200)!

    //Setup the image context using the passed image.
    UIGraphicsBeginImageContext(inImage.size)

    //Setups up the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: UIColor.redColor(),
    ]

    let textFontAttributes1 = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: UIColor.greenColor(),
    ]


    let textFontAttributes2 = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: UIColor.purpleColor(),
    ]


    let textFontAttributes3 = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: UIColor.yellowColor(),
    ]


    //Put the image into a rectangle as large as the original image.
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    // Creating a point within the space that is as bit as the image.
    let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    //Now Draw the text into an image.
    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    //text 1
    let rect1: CGRect = CGRectMake(atPoint1.x, atPoint1.y, inImage.size.width, inImage.size.height)
    drawText1.drawInRect(rect1, withAttributes: textFontAttributes1)

    //text 2
    let rect2: CGRect = CGRectMake(atPoint2.x, atPoint2.y, inImage.size.width, inImage.size.height)
    drawText2.drawInRect(rect2, withAttributes: textFontAttributes2)

    //text 3
    let rect3: CGRect = CGRectMake(atPoint3.x, atPoint3.y, inImage.size.width, inImage.size.height)
    drawText3.drawInRect(rect3, withAttributes: textFontAttributes3)


    // Create a new image out of the images we have created
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    // End the context now that we have the image we need
    UIGraphicsEndImageContext()

    //And pass it back up to the caller.
    return newImage
}
并从当前正在执行的位置调用该方法,但如下所示

ImageDisplay.image = textToImage(CustomerTextBox.placeholder!, drawText1: ResponsibleTextBox.placeholder!, drawText2: LocationTextBox.placeholder!, drawText3: DescriptionTextBox.placeholder!, inImage: image, atPoint: CGPoint( x: 400, y: 100), atPoint1: CGPoint( x: 600, y: 300), atPoint2: CGPoint( x: 400, y: 600), atPoint3: CGPoint( x: 600, y: 1000));
这里的测试点是静态的,根据您的需要进行更改

输出:

希望能有帮助


快乐编码…

您想在选择图像后并在imageView中显示时显示文本,还是想在从gallery中选择图像时显示一些文本?@Janmenjaya after selection很好enough@Janmenjaya我已经更新了我的问题。请参阅我的答案并解释您的问题。