ios - 在 iPhone 上拍照并在 Swift 中保存之前永久覆盖文本

标签 ios iphone swift2

我刚刚开始编程。在保存图像之前,我试图在用 iPhone 拍摄的图像上叠加文本。我发现了一些看起来很有前途的代码,但没有任何具体的工作,也没有具体回答问题的帖子。这是我拍摄图像的代码。无需尝试叠加文字,我知道它会拍照。

@IBAction func handleShutterButton(sender: UIButton) {
    cameraController.captureStillImage { (image, metadata) -> Void in
        self.view.layer.contents = image
        var project = "ProjectX"
        var camNumber = "c01"
        let timestamp = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
        let overlay = "\(project) \(camNumber) \(timestamp)"
        self.overlayText(overlay, inImage: UIImage(named: image)!, atPoint: CGPointMake(20, 20)) // This is where it fails
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
}

这是我用来覆盖文本的函数。

func overlayText(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

    // Setup the font specific variables
    let textColor: UIColor = UIColor.whiteColor()
    let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 12)!

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

如有任何帮助,我们将不胜感激。

最佳答案

我已经修改了你的代码..试试这个..

@IBAction func handleShutterButton(sender: UIButton) {

    cameraController.captureStillImage { (image, metadata) -> Void in
        self.view.layer.contents = image
        var project = "ProjectX"
        var camNumber = "c01"
        let timestamp = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
        let overlay = "\(project) \(camNumber) \(timestamp)"
        var changedImage : UIImage = self.overlayText(overlay, inImage: image, atPoint: CGPointMake(20, 20)) // This is where it fails
        UIImageWriteToSavedPhotosAlbum(changedImage, nil, nil, nil);
    }
}

关于ios - 在 iPhone 上拍照并在 Swift 中保存之前永久覆盖文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360244/

相关文章:

ios - Swift - 日期格式(DD 与 dd)

设备过热的 iOS 通知

iphone - 如何在绘图应用程序中撤消和重做操作

ios - Swift 2 arc4random_uniform 崩溃?

ios - XCUIApplication().debugDescription 的打印输出使 UIAutomated Test XCode 8 崩溃

ios - 按钮点击错误

ios - Xcode 10.2 GNU 风格的内联汇编被禁用

iphone - 关于使用 splitview Controller 的应用程序的最佳实践,我的应用程序可以被拒绝吗?

swift - Swift 2.0新的'执行更改'PHPPhotoLibrary将无法正常工作

ios - 强制图层不在选项卡栏中旋转