Swift3 Swift 3绘图应用程序中的错误

Swift3 Swift 3绘图应用程序中的错误,swift3,drawing,xcode8,Swift3,Drawing,Xcode8,我正在使用Xcode 8在Swift 3中制作一个绘图应用程序。我试着遵循一个教程,但似乎无法在该网站的评论部分找到这些错误的答案 以下是我经常遇到的两组错误: context?.setLineCap(kCALineCapRound) context?.setLineWidth(brushWidth) context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0) context?.setBlendMode(kCGB

我正在使用Xcode 8在Swift 3中制作一个绘图应用程序。我试着遵循一个教程,但似乎无法在该网站的评论部分找到这些错误的答案

以下是我经常遇到的两组错误:

context?.setLineCap(kCALineCapRound)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(kCGBlendModeNormal)

UIGraphicsBeginImageContext(mainImageView.frame.size)
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()


如果有人能帮我找出如何排除这两个错误,我将不胜感激。

如果您发现代码中有错误,请检查

宣言

枚举

CGLineCap 您需要将
CGLineCap
值传递给
setLineCap(:)
方法,在Swift 3中它是enum,因此您需要传递一个enum case

这一行:

context?.setLineCap(kCALineCapRound)
应该是这样的:

context?.setLineCap(CGLineCap.round)
context?.setLineCap(.round)
或者简单地说:

context?.setLineCap(CGLineCap.round)
context?.setLineCap(.round)
(顺便说一句,
kcalinecapound
不是
setLineCap
--
CGContextSetLineCap
的有效常数,即使在以前的Swift和Objective-C中也是如此。它应该是)


再次

宣言

枚举

CGBlendMode 更改此行:

context?.setBlendMode(kCGBlendModeNormal)
为此:

context?.setBlendMode(CGBlendMode.normal)
或者这个:

context?.setBlendMode(.normal)
第二组错误也需要类似的更改(以及一些简单的修复)



你最好花些时间来寻找一个好的Swift 3教程,否则你可能会花更多的时间来学习如何将代码从Swift 2迁移到Swift 3,而不是在Swift 3中创建应用程序。

在你提到的教程评论中,我在以下链接找到了一个Swift 3更新:

基于该项目,需要修复的两个代码块将变成:

context?.setLineCap(.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(.normal)


我知道可能已经晚了,但我希望这也能帮助其他人,因为我自己也试图修复这些错误,但由于Xcode的一些错误提示而陷入困境,直到我在链接中找到github项目

谢谢你的反馈。我是第一次学习编码的学生,很难找到合适的教程,也很难找到从swift 2到swift 3的转换。反馈帮助有关更多信息,您可以在github上的以下位置找到我的代码:
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: 1.0)
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()