2016-12-07 13 views
-4

私はiOSアプリケーションでInstagramに画像を投稿する実装を試みています。私はこのポストでコードを使用しました。'String'型の値を 'URL'の引数型に変換できません

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

:スウィフト3用

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

が、その後、私はエラーを取得する:

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

ドウ私はラインを変更する必要が

Post UIImage to Instagram using Swift similar to SLComposeViewController

誰でもこれを解決する方法を知っていますか?

ありがとうございました。

+1

を見てみましょう。 – Wolverine

+0

jpgPathとは –

答えて

1

これを試してください。

let jpegData = UIImageJPEGRepresentation(imageInstagram, 1.0) 

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

そしてここjpgPathは(あなたのケースである)文字列ある

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) 
} 

場合は、そのパスのURLを作成する方法の例ではありません場合は、あなたができますこのようにしてください。

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

あなたは、書き込み関数は、引数としてURLを取るように無効な引数で関数を記述するために、引数としてstringを渡しています。

だから、単にこれに

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

を置き換えます。詳細については

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

をあなたの代わりにURLの文字列を渡しているthis

+1

https://developer.apple.com/reference/foundation/nsdata/1410595-writeを意味しましたか? (あなたのリンクは 'write(toFile:options:)'関数について説明しています –

+0

@ŁukaszPrzytułaありがとうございます。私はリンクを交換しました。 –

+0

とにかくリンクしたものはまだ便利です(URLを文字列で初期化する必要はありません。パラメータとして文字列を渡すだけです)。 –

関連する問題