2017-06-21 1 views
1
let profile_pic_url_hd = user["profile_pic_url_hd"] as! String 
self.imgURL = "\(profile_pic_url_hd)" 

self.imgURLはリンクであり、文字列です。リンクは、例えば、次のとおりです。/s320x320/せず、言い換えればリンクがある文字列を変更する

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/19121160_1328742810566926_6482637033138290688_a.jpg 

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/s320x320/19121160_1328742810566926_6482637033138290688_a.jpg 

は、誰もがこのリンクを変更する方法を知っているん。

+0

なぜあなたは "\(profile_pic_url_hd)" '代入されている' 'self.imgURL'の代わりに、直接' profile_pic_url_hd'を割り当てますか? – rmaddy

+0

'profile_pic_url_hd'はJsonクラスまたはそれが呼び出されたものの名前です –

+0

いいえ、そうではありません。これは 'String'型の変数です。ただしてください: 'self.imgURL = profile_pic_url_hd' – rmaddy

答えて

0

URLComponentsを使用してURLのパスを分割し、新しいURLを再構築することができます。

let urlstr = "https://scontent-frx5-1.cdninstagram.com/t51.2885-19/s320x320/19121160_1328742810566926_6482637033138290688_a.jpg" 
if var comps = URLComponents(string: urlstr) { 
    var path = comps.path 
    var pathComps = path.components(separatedBy: "/") 
    pathComps.remove(at: 2) // this removes the s320x320 
    path = pathComps.joined(separator: "/") 
    comps.path = path 
    if let newStr = comps.string { 
     print(newStr) 
    } 
} 

出力:

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/19121160_1328742810566926_6482637033138290688_a.jpg 
+0

ありがとう! –

関連する問題