2016-12-07 89 views
-4

我试图在iOS应用中实现向Instagram发布图像。我在这篇文章中使用了代码。无法将类型'String'的值转换为期望的参数类型'URL'

Post UIImage to Instagram using Swift similar to SLComposeViewController

我需要更改线路:

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

到:

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

的斯威夫特3,但当时我得到的错误:

Cannot convert value of type 'String' to expected argument type 'URL'

李四有人知道如何解决这个问题吗?

谢谢。

+1

你逝去的字符串,而非网址。 – Wolverine

+0

什么是jpgPath –

回答

1

试试这个。

let jpegData = UIImageJPEGRepresentation(imageInstagram, 1.0) 

As I consider "jpgPath" is PATH where you are trying to save image.

如果没有那么这里是一个例子

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字符串(这是你的情况下),你应该如何创建一个PATH URL,就可以像这样尝试。

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

你传入string作为参数写的函数,它是无效的参数作为写功能需要URL作为参数。

所以,简单地更换

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

与此:

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

有关详细信息看看this

+1

您的意思是https://developer.apple.com/reference/foundation/nsdata/1410595-write? (你的链接描述了'write(toFile:options:)'func) –

+0

@ŁukaszPrzytuła谢谢你指出它。我已经取代了这个链接。 –

+0

无论如何,你链接到的那个仍然有用(你不需要用字符串初始化URL,只需传递字符串作为参数) –

相关问题