2016-09-27 10 views
1

変数に複数の値を割り当てたいとします。どうやってやるのか分かりません。基本的には、画像に複数のテキストを入れたいと思っています。スウィフトでの複数の割り当て

これは(正常に動作している)単一代入コードです:

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に多くの値を割り当てます。これはtextToImage機能です

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) 

} 

:ここで私は(間違っている)の例を示すことができる

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 
} 
+0

ここで、textToImageはありますか? –

+0

@UmairAfzal私はtextToImage関数で更新しました – coskukoz

答えて

1

たびtextToImageを "image"で呼び出すと、元のイメージから指定された文字列を持つ新しいオブジェクトが作成されますが、必要なものは以前の各ビルドイメージに文字列を追加することです。 これを行うことができます:

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) 
} 
+0

最も簡単な方法。正常に動作します – coskukoz

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を新しいものに変更してください。

EDIT このコードはWORKS(私はそれをテストしている):

//..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 
    } 
+0

あなたは正しいです__ImageDisplay.image__は最後のデータだけを保存します。しかし、私はすべてのテキストを1つの画像に表示したい。 – coskukoz

+0

w8私はchanginファンクションコード – Vyacheslav

+0

@ coskukoz doneです。 – Vyacheslav

1

私の推測では、チェーンに一時的UIImagetextToImage(...)への呼び出しのようになります。

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) 
} 
関連する問題