如何在拍摄的照片上画线?

时间:2016-09-28 06:59:10

标签: ios swift

我想在选择/拍摄照片后在文字下画出白线。我看到了一些例子,但它们不能用我的代码,所以我需要问。基本上在图片之后,图片顶部会出现白色宽线。我想我会在 textToImage 函数中编写 drawRect 。我不确定。

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

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

@IBOutlet weak var CustomerTextBox: UITextField!
@IBOutlet weak var ResponsibleTextBox: UITextField!
@IBOutlet weak var LocationTextBox: UITextField!
@IBOutlet weak var DescriptionTextBox: UITextField!



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 CameraAction(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]) {

    if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        image = textToImage(CustomerTextBox.text!, inImage: image, atPoint: CGPoint( x: 100, y: 50))
        image = textToImage(ResponsibleTextBox.text!, inImage: image, atPoint: CGPoint( x: 1000, y: 50))
        image = textToImage(LocationTextBox.text!, inImage: image, atPoint: CGPoint( x: 3000, y: 50))
        image = textToImage(DescriptionTextBox.text!, inImage: image, atPoint: CGPoint( x: 100, y: 200))
        ImageDisplay.image = image
    }
    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: 150)!

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


}

我找到了here的内容 我将在下面添加新功能:

    func drawRect(rect: CGRect)
{
    let context = UIGraphicsGetCurrentContext()
    CGContextMoveToPoint(context, 100, 100)
    CGContextAddLineToPoint(context, 150, 150)
    CGContextAddLineToPoint(context, 100, 200)
    CGContextAddLineToPoint(context, 50, 150)
    CGContextAddLineToPoint(context, 100, 100)
    CGContextSetFillColorWithColor(context,
                                   UIColor.redColor().CGColor)
    CGContextFillPath(context)
}

我不知道我应该在这里写些什么:

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

    if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        image = textToImage(CustomerTextBox.text!, inImage: image, atPoint: CGPoint( x: 100, y: 50))
        image = textToImage(ResponsibleTextBox.text!, inImage: image, atPoint: CGPoint( x: 1000, y: 50))
        image = textToImage(LocationTextBox.text!, inImage: image, atPoint: CGPoint( x: 3000, y: 50))
        image = textToImage(DescriptionTextBox.text!, inImage: image, atPoint: CGPoint( x: 100, y: 200))
        image = drawRect("WHATWILLBEHERE")
        ImageDisplay.image = image
    }
    dismissViewControllerAnimated(true, completion: nil)
}

1 个答案:

答案 0 :(得分:1)

这适合我,但我使用了swift 3,我认为你可以在swift 2中转换它

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

        ImageDisplay.image = image

        if ImageDisplay.layer.sublayers != nil {

        for layer : CALayer in ImageDisplay.layer.sublayers! {
            layer.removeFromSuperlayer()
        }
        }

        textToImage(drawText: CustomerTextBox.text!, inImage: ImageDisplay, atPoint: CGPoint(x: ImageDisplay.bounds.midX, y: ImageDisplay.bounds.midY))


    }
    dismiss(animated: true, completion: nil)


}

 func lineUnderText(myTextPosition: CGRect)  {

    let path = UIBezierPath()

    path.move(to: CGPoint(x: myTextPosition.minX, y: myTextPosition.maxY + 4))
    path.addLine(to: CGPoint(x: myTextPosition.maxX, y: myTextPosition.maxY + 4))

    let shape = CAShapeLayer()

    shape.path = path.cgPath
    shape.strokeColor = UIColor.white.cgColor
    shape.fillColor = UIColor.clear.cgColor
    shape.lineWidth = 2
    shape.lineCap = kCALineCapRound

    ImageDisplay.layer.addSublayer(shape)


        }




func textToImage(drawText: String, inImage: UIImageView, atPoint:CGPoint){

    let myLabel = UILabel()
    myLabel.text = drawText
    myLabel.textColor = UIColor.black
    myLabel.font = UIFont(name: "Helvetica Bold", size: 25)
    print(drawText.characters.count)
    let W = myLabel.intrinsicContentSize.width
    let H = myLabel.intrinsicContentSize.height
    myLabel.frame = CGRect(x: atPoint.x - W/2 , y: atPoint.y - H/2, width: W, height: 28)

   inImage.addSubview(myLabel)

    lineUnderText(myTextPosition: myLabel.frame)

}

OK Swift 2

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

        ImageDisplay.image = image

        if ImageDisplay.layer.sublayers != nil {

            for layer : CALayer in ImageDisplay.layer.sublayers! {
                layer.removeFromSuperlayer()
            }
        }

        textToImage(CustomerTextBox.text!, inImage: ImageDisplay, atPoint: CGPoint(x: ImageDisplay.bounds.midX, y: ImageDisplay.bounds.midY))


    }
    dismissViewControllerAnimated(true, completion: nil)


}

 func lineUnderText(myTextPosition: CGRect)  {

    let path = UIBezierPath()

    path.moveToPoint( CGPoint(x: myTextPosition.minX, y: myTextPosition.maxY + 4))
    path.addLineToPoint( CGPoint(x: myTextPosition.maxX, y: myTextPosition.maxY + 4))

    let shape = CAShapeLayer()

    shape.path = path.CGPath
    shape.strokeColor = UIColor.whiteColor().CGColor
    shape.fillColor = UIColor.clearColor().CGColor
    shape.lineWidth = 2
    shape.lineCap = kCALineCapRound

    ImageDisplay.layer.addSublayer(shape)


}




func textToImage(drawText: String, inImage: UIImageView, atPoint:CGPoint){

    let myLabel = UILabel()
    myLabel.text = drawText
    myLabel.textColor = UIColor.blackColor()
    myLabel.font = UIFont(name: "Helvetica Bold", size: 25)

    let W = myLabel.intrinsicContentSize().width
    let H = myLabel.intrinsicContentSize().height
    myLabel.frame = CGRect(x: atPoint.x - W/2 , y: atPoint.y - H/2, width: W, height: 28)

    inImage.addSubview(myLabel)

    lineUnderText( myLabel.frame)

}

enter image description here

我希望能帮到你