Swift上的多个赋值

时间:2016-09-27 12:38:29

标签: ios swift

我想为变量分配多个值。我不知道该怎么做。基本上我想在图像上放置多个文本。

这是单一的分配代码(工作正常):

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))

    }
    dismissViewControllerAnimated(true, completion: nil)

}

我只想为 ImageDisplay.image 分配更多价值。在这里,我可以展示一个(错误的)示例:

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
        ImageDisplay.image = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
        ImageDisplay.image = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))            
    }
    dismissViewControllerAnimated(true, completion: nil)

}

这是textToImage函数:

    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
}

3 个答案:

答案 0 :(得分:1)

我的猜测是将textToImage(...)上的UIImage来电串联起来:

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        var tmpImg = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
        tmpImg = textToImage("HERE IS SECOND LABEL", inImage: tmpImg, atPoint: CGPoint( x: 400, y: 200))
        tmpImg = textToImage("HERE IS THIRD LABEL", inImage: tmpImg, atPoint: CGPoint( x: 400, y: 300))  
        ImageDisplay.image = tmpImg
    }
    dismissViewControllerAnimated(true, completion: nil)
}

答案 1 :(得分:1)

每次使用" image"调用textToImage时您正在使用原始图像中提供的字符串创建新对象,但您想要的是将字符串添加到每个先前构建的图像。 所以你可以这样做:

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

   if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
     image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
     image = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
     image = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))

     ImageDisplay.image = image            
  }
  dismissViewControllerAnimated(true, completion: nil)
}

答案 2 :(得分:0)

您的代码意味着:

let a = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
ImageDisplay.image = a
let b = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
ImageDisplay.image = b
let c = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))
ImageDisplay.image = c

您的ImageDisplay.image仅存储最后一个数据(c变量)

ab保存在某处,以便以后再使用。 或将textToImage功能更改为新功能。

修改 这个代码工作(我已经测试过了):

//..somewhere
   guard let img = UIImage(named:"telephone40.png") else {
            return
        }//UIImage.init()
        //        img.size = CGSizeMake(500,500)
        var tmpImg = textToImage(["HERE IS FIRST LABEL","HERE IS SECOND LABEL","HERE IS THIRD LABEL"], inImage: img, atPoints: [CGPoint( x: 0, y: 10),CGPoint( x: 20, y: 30),CGPoint( x: 40, y: 50)])
//result <<<<    

//function:

    func textToImage(drawTexts: [NSString], inImage: UIImage, atPoints:[CGPoint])->UIImage{

        // Setup the font specific variables
        let textColor: UIColor = UIColor.blackColor()
        let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 200)!
        //Setups up the font attributes that will be later used to dictate how the text should be drawn

        let textFontAttributes = [
            NSFontAttributeName: textFont,
            NSForegroundColorAttributeName: textColor,
            ]
        //Setup the image context using the passed image.
        UIGraphicsBeginImageContext(inImage.size)
        //Put the image into a rectangle as large as the original image.
        inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
        for i in 0..<drawTexts.count {
            let drawText = drawTexts[i]
            let atPoint = atPoints[i]



            // 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
    }