ios - 在图像上的文本下绘制一个矩形然后保存

标签 ios swift xcode

我目前正在开发相机应用程序。我已经实现了在图像上写文字,但是有一个问题,有时仅仅因为图片的颜色很深,很难阅读文字。所以我想在文本下面画一个白色的矩形。

其实我找到了如何在图片中绘制一个矩形,但是我无法在文本下绘制它,而且保存后矩形也没有出现。

MainStoryBoard Picture

下面的代码用于绘制矩形:

     let imageSize = CGSize(width: ImageDisplayWidthConstantSize*0.75, height: 50)
     let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 20, y: 215), size: imageSize))
     self.view.addSubview(imageView)
     let imageBlank = drawCustomImage(imageSize)
     imageView.image = imageBlank

    func drawCustomImage(size: CGSize) -> UIImage {
    // Setup our context
    let bounds = CGRect(origin: CGPoint.zero, size: size)
    let opaque = true
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
    let context = UIGraphicsGetCurrentContext()

    // Setup complete, do drawing here
    CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextSetLineWidth(context, 50)

    CGContextStrokeRect(context, bounds)

    CGContextBeginPath(context)
    CGContextMoveToPoint(context, CGRectGetMinX(bounds), CGRectGetMinY(bounds))
    CGContextAddLineToPoint(context, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))
    CGContextMoveToPoint(context, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))
    CGContextAddLineToPoint(context, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))
    CGContextStrokePath(context)

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

此代码用于在图片上插入文字:

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

    if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let ScreenSize: CGRect = UIScreen.mainScreen().bounds
        let ImageDisplayWidthConstantSize = ScreenSize.width

        let imageSize = CGSize(width: ImageDisplayWidthConstantSize*0.75, height: 50)
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 20, y: 215), size: imageSize))
        self.view.addSubview(imageView)
        let imageBlank = drawCustomImage(imageSize)
        imageView.image = imageBlank

        image = textToImage(CustomerTextBox.text!, inImage: image, atPoint: CGPoint( x: 50, y: 0)) //Customer Text Field
        image = textToImage(ResponsibleTextBox.text!, inImage: image, atPoint: CGPoint( x: 1000, y: 0)) //Responsible Text Field
        image = textToImage(LocationTextBox.text!, inImage: image, atPoint: CGPoint( x: 2200, y: 0)) //Location Text Field
        image = textToImage(DescriptionTextBox.text!, inImage: image, atPoint: CGPoint( x: 50, y: 200)) //Description Text Field

        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: 100)!

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

最佳答案

这是一个可能的解决方案,只需添加到 textFontAttributes NSBackgroundColorAttributeName : UIColor.white (Swift 3)。 swift 2 UIColor.whiteColor()

let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        NSBackgroundColorAttributeName : UIColor.white 
        ]

现在您可以调用函数textToImage 来绘制文本和矩形enter image description here

关于ios - 在图像上的文本下绘制一个矩形然后保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40216816/

相关文章:

iphone - 使用带有iOS模拟器的内置相机的mac book

ios - 由于未捕获的异常'NSInvalidArgumentException Xcode 9.1/iPhone X而终止应用程序

ios - misaligned_stack_error_是什么意思?

ios - MFMessageComposeViewController canSendText 类在模拟器上返回 YES

ios - 快速错误线程 1 : EXC_BAD_INSTRUCITON

ios - 在 iOS 中检查我的应用程序的电池消耗

ios - 游戏场景不显示

ios - 将子层添加到 UICollectionViewCell 时出现意外行为

ios - Xcode swift 发送带截图的邮件?

ios - Mapkit - Swift4 - 如何禁用自动缩放