【问题标题】:How to Insert Text on Taken Picture by Swift?如何在 Swift 拍摄的照片上插入文字?
【发布时间】:2016-09-22 08:16:29
【问题描述】:

我正在 Xcode 上制作相机应用程序。我制作了“照片库”和“相机”选项来将照片插入屏幕。之后我只想在图片上写文字。文本可以来自用户,就像使用教科书一样。所以基本上我需要在图片上添加文本,比如日期戳。

我没有找到任何示例,因此它对开发人员很有帮助。希望达到目标。

这是我的屏幕:

这是我的代码:

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

【问题讨论】:

  • 可以参考这个thread
  • @woogii 我正在尝试,但我无法定义文本。我的意思是如何编写要插入图片的文本?如果我可以定义一些文本,我想它会起作用。

标签: ios swift xcode core-graphics


【解决方案1】:

我想你可以先从照片库中获取图像,然后将其传递给将文本绘制到图像中的函数。

 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)

}

【讨论】:

  • 也许很容易出错,但我不是很擅长,所以我要问。我看到这些错误: *一行上的连续声明必须用';'分隔* 预期的声明 *'let' 声明不能是计算属性 *计算属性必须具有显式类型 *带有 getter/setter 的变量不能有初始值
  • 你能根据你的问题更新你的代码吗?我会调查的。
  • @CoşkunKozakbaş 我更新了我的答案。试试那个代码。可选绑定语句应位于 UIImagePickerController 委托方法中,以便您可以将选定的图像作为参数传递给“textToImage”函数。
猜你喜欢
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 2018-10-07
  • 1970-01-01
相关资源
最近更新 更多