2016-09-22 8 views
1

私はXcodeでカメラアプリを作っています。私は '写真ライブラリ'と 'カメラ'オプションを使って写真を画面に挿入しました。その後、私は絵にテキストを書きたいだけです。テキストは、教科書を使用するようにユーザーから入力することができます。だから基本的に私は写真に日付スタンプのようなテキストを追加する必要があります。Swiftによる撮影画像にテキストを挿入するには?

私はこれのための例を見つけませんでしたので、開発者の役に立つでしょう。目標を達成することを願っています。

これは私の画面です:

enter image description here

、これは私のコードです:

import UIKit 

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

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

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 CameraActıon(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]) { 
    ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage; 
    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: 35)! 

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

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
    ImageDisplay.image = textToImage("TextYouWantDraw", inImage: image, atPoint: CGPointMake(10,10))} 
} 
+0

この[スレッド](http://stackoverflow.com/a/28907930/5657370)を参照してください。 – woogii

+0

@woogii私はそれを試しているが、私はテキストを定義することはできません。私はどのように画像に挿入するテキストを書くことができますか?私はいくつかのテキストを定義することができればうまくいくと思う。 – coskukoz

答えて

2

私はあなたが最初のフォトライブラリから画像を取得し、その関数に渡すことができますね画像にテキストを描画します。

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

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
    ImageDisplay.image = textToImage("TextYouWantDraw", inImage: image, atPoint: CGPointMake(10,10)) 
} 

    dismissViewControllerAnimated(true, completion: nil) 

} 
+0

多分簡単なエラーですが、私は本当にうまくいきませんので、私は尋ねるつもりです。私はこれらのエラーが表示されます: *行の連続宣言は ';'で区切られなければなりません。 *宣言 *「を聞かせて」期待宣言は計算できない性質 は*計算さプロパティは、あなたの質問に、あなたのコードを更新することができ、初期値 – coskukoz

+0

を持つことができないゲッター/セッターとの明示的な型 *変数を持っている必要がありますか?私はそれを徹底的に調べます。 – woogii

+0

私はコードを更新しました。 – coskukoz

関連する問題