2016-09-22 46 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

也許這麼容易出錯,但我並不擅長,所以我會問。我看到這些錯誤: *一行中的連續聲明必須用';'分隔 *預期聲明 *'讓'聲明無法計算屬性 *計算屬性必須有明確的類型 * getter/setter變量不能有初始值 – coskukoz

+0

您可以更新您的問題上的代碼?我會仔細看看的。 – woogii

+0

我已經更新了代碼。 – coskukoz