2016-09-22 3 views
1

저는 Xcode에서 카메라 앱을 만들고 있습니다. '사진 라이브러리'와 '카메라'옵션을 사용하여 사진을 화면에 삽입했습니다. 그 후에 나는 그림에 텍스트를 쓰고 싶다. 텍스트는 교과서를 사용하는 것처럼 사용자가 올 수 있습니다. 그래서 기본적으로 나는 그림에 datestamp와 같은 텍스트를 추가해야합니다.촬영 한 사진에 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

이 [thread] (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

어쩌면 그렇게 쉬운 오류 일지 모르지만 나는 그것에 익숙하지 않아서 물어볼 것입니다. 다음 오류가 표시됩니다. * 한 줄의 연속 선언은 ';'으로 구분해야합니다. * 예상 선언 * 'let'선언은 계산할 수 없습니다. * 계산 된 속성은 명시적인 형식이어야합니다. * getter/setter를 사용하는 변수는 초기 값을 가질 수 없습니다. – coskukoz

+0

질문에 대한 코드를 업데이트 할 수 있습니까? 나는 그것을 조사 할 것이다. – woogii

+0

코드를 업데이트했습니다. – coskukoz

관련 문제