swift - 第一次保存图像后,我的图像保存功能中的几行不运行

标签 swift swift3

我正在制作一个应用程序,允许您在图像上显示 TextView ,然后将其另存为全新图像。我在将图像上方的新扩展区域保持为白色以模拟 TextView 时遇到问题。

这是该应用程序的基础:

app basis

这是我第一次保存后的样子:

looks first time

这是我在应用程序打开时多次保存后的样子:

while app is open

(忽略模拟器截图的白色边框)

这是我的保存功能,它将保存的图像扩展为与 TextView (如果它主动显示)相同的数量,然后将扩展区域着色为白色。然后它 super 将 textview 文本强加在白色上。出于某种原因,它只会在第一次保存图像时在扩展区域中涂上白色。之后 backgroundColor.setFill() 不会激活,如上面的屏幕截图所示:

 @IBAction func save(_ sender: Any) {
    guard image != nil else { return }

    let offset = scrollView.contentOffset
    let screenHeight = screenSize.height

    let normalSize = CGSize(width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)
    let textViewWithImageSize = CGSize(width: scrollView.bounds.size.width, height: ((screenHeight * 0.17) + scrollView.bounds.size.height))
    var yPos: Int = Int(-offset.y)

    //if textView is active or not and the different y position for saved image to include the textview if it is active
    // screenHeight * 0.17 is the dynamic height of the textview per device.

    if textView.isHidden == true {
        UIGraphicsBeginImageContextWithOptions(normalSize, true, UIScreen.main.scale)
        textView.text = ""
        yPos = Int(-offset.y)
    }else if textView.isHidden == false {
        yPos = Int(-offset.y + (screenHeight * 0.17))
        UIGraphicsBeginImageContextWithOptions(textViewWithImageSize, true, UIScreen.main.scale)

        //sets saves text area background to white, extendind the image area on its own colored the void black.
        let backgroundColor: UIColor = UIColor.white
        backgroundColor.setFill()
        UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: (offset.y - (screenHeight * 0.17)), width: scrollView.bounds.size.width, height: scrollView.bounds.size.height))
    }

    UIGraphicsGetCurrentContext()!.translateBy(x: -offset.x, y: CGFloat(yPos))
    scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)

    let textColor = UIColor.red
    let textFont = UIFont(name: "Helvetica", size: 14)!
    let textFontAttributes = [NSFontAttributeName: textFont,NSForegroundColorAttributeName: textColor] as [String : Any]
    let rect = CGRect(x: offset.x + 5, y: (offset.y + 5 - (screenHeight * 0.17)), width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)

    textView.text.draw(in: rect, withAttributes: textFontAttributes)

    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
    centerScrollViewContents()

 let alert = UIAlertController(title: "Image Saved", message: "your image has been saved", preferredStyle: .alert)
    self.present(alert, animated: true, completion: nil)
    alert.addAction(UIAlertAction(title: "Neat", style: .default, handler: nil))
}

上面是我的整个保存和渲染图像功能,下面是我用来切换textview是否存在的简单大小写开关:

   @IBAction func frame(_ sender: Any) {
     guard image != nil else { return }


    switch currentFrame{
    case 0:
        textView.isHidden = true
    case 1:
        textView.isHidden = false
    default:
        break
    }
    currentFrame += 1
    if currentFrame > 1 {
        currentFrame = 0
    }
}

我想象根据 TextView 是否隐藏在保存函数中设置 if 语句将是最干净的解决方案,这实际上似乎有效。但是

backgroundColor.setFill()

在初始保存后由于某种原因被绕过,但图像仍然延伸并且 super 正确地强加了文本。

我很茫然,如有任何建议,我们将不胜感激。

最佳答案

我找到了。这一行:

UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: (offset.y - (screenHeight * 0.17)), width: scrollView.bounds.size.width, height: scrollView.bounds.size.height))

y: 需要

y: 0 - (screenHeight * 0.17))

而不是像我试图做的那样尝试匹配 ScrollView 图像偏移。

关于swift - 第一次保存图像后,我的图像保存功能中的几行不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550939/

相关文章:

ios - 加载后 Swift UITableView 消失

ios - 有没有办法访问在 Swift 的 CollectionView 中显示最多的单元格?

swift - 在 Swift 中获取远程文件顶部

javascript - IOS WKWebView 和 UIWebView 没有加载 JavaScript 注册表单,而 Safari 可以

ios - 选择器函数未在 iOS Swift 中调用

IOS Swift3 卡叠叠加

ios - UIImagePickerController 崩溃应用程序 | swift 3,Xcode8

swift - 整数文字存储到 'Int' 时溢出

swift - 解析/Heroku 和 Cocoapods

swift - 解决 Swift 3 中缺少递归协议(protocol)约束的问题