2016-12-07 2 views
-4

iOS 앱에서 Instagram에 이미지를 게시하도록 구현하려고합니다. 나는이 게시물에서 코드를 사용했다.'String'유형의 값을 'URL'의 예상 인수 유형으로 변환 할 수 없습니다.

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

에 :

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

미상 :

UIImageJPEGRepresentation(imageInstagram, 1.0)!.write(jpgPath, options: true) 
스위프트 3

하지만 그때 나는 오류가

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을 초기화하지 않고 문자열을 매개 변수로 전달하면됩니다.) –

관련 문제