Ios 无法转换类型为';字符串';到预期的参数类型';URL';

Ios 无法转换类型为';字符串';到预期的参数类型';URL';,ios,swift3,xcode8,nsurl,Ios,Swift3,Xcode8,Nsurl,我正在尝试在iOS应用程序中向Instagram发布图像。我在这篇文章中使用了代码 我需要更改线路: UIImageJPEGRepresentation(imageInstagram, 1.0)!.writeToFile(jpgPath, atomically: true) 致: 对于Swift 3,我得到了一个错误: 无法将“字符串”类型的值转换为预期的参数类型“URL” 有人知道怎么解决这个问题吗 谢谢。试试这个 let jpegData = UIImageJPEGRepresentat

我正在尝试在iOS应用程序中向Instagram发布图像。我在这篇文章中使用了代码

我需要更改线路:

UIImageJPEGRepresentation(imageInstagram, 1.0)!.writeToFile(jpgPath, atomically: true)
致:

对于Swift 3,我得到了一个错误:

无法将“字符串”类型的值转换为预期的参数类型“URL”

有人知道怎么解决这个问题吗

谢谢。

试试这个

let jpegData = UIImageJPEGRepresentation(imageInstagram, 1.0)

我认为“JPGPATH”是您试图保存图像的路径。

如果没有,那么下面是一个示例,您应该如何创建该路径URL

let jpgPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("instagram.jpg")

do {
    try jpegData.write(to: jpgPath, options: .atomic)
} catch {
    print(error)
}
如果jpgPath是一个字符串(在您的例子中),您可以这样尝试

do {
    try data.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)
} catch {
    print(error)
}

您正在将
string
作为参数传递给write函数,该函数的参数无效,因为write函数将
URL
作为参数

所以简单地替换

UIImageJPEGRepresentation(imageInstagram, 1.0)!.write(jpgPath, options: true)
为此:

UIImageJPEGRepresentation(imageInstagram, 1.0)!.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)

有关更多详细信息,请查看

您正在传递的是字符串而不是URL。什么是JPGPathWhat?(您的链接描述了
write(toFile:options:)
func)@ukaszPrzytuł感谢您的指点。我已经替换了链接。无论如何,您链接到的链接仍然有用(您不必使用字符串初始化URL,只需将字符串作为参数传递即可)@VishalSonawane当我按照您的建议进行操作时,我收到错误消息“Call can throw,但它没有标记为“try”,并且错误未得到处理”。我把你的建议和金刚狼的建议结合在一起,这让我通过了这个错误。非常感谢。
UIImageJPEGRepresentation(imageInstagram, 1.0)!.write(to: URL(fileURLWithPath: jpgPath), options: .atomic)