在没有URL的swift 4中从字符串中在imageview中显示图像

时间:2018-10-08 18:30:14

标签: get uiimage swift4

我在swift 4中有一个项目。我需要从api获取图像,但出现错误,因为“无法将'String'类型的值分配给'UIImage?'。”。我的项目代码为:< / p>

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {


  @IBOutlet weak var coachProfile: UIImageView!

   var responseArr = [JSON]()

    override func viewDidLoad() {
        super.viewDidLoad()

      getCoachList()
    }

  func getCoachList() {
    let url = "http://test.ilovecoach.com/api/student"
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let loader = appDelegate.showLoading()
    WebServices.sharedInstance.get(toURL: url, successHandler: { (jsonData) in
      print("messLists: \(jsonData)")
     self.responseArr  = jsonData.arrayValue
     loader.removeFromSuperview()
      let avatar = self.responseArr[0]["photo"].stringValue
       print("CoachAvatar",avatar) // gives: 12.png

      self.coachProfile.image = avatar //gives error as:Cannot assign value of type 'String' to type 'UIImage?'

    }) { (error) in 
    }
  }
}

我有一个get方法api,它的响应为:

[
    {
"name" : "Modi",
    "title" : "d'Etat",
    "id" : 12,
    "photo" : "12.png"
}
]

现在,我需要获取名为“ photo”的图像到coachprofile imageview。如何在Swift 4中解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用此:

Add-Type -Path "WinSCPnet.dll"

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::S3
    HostName = "s3.amazonaws.com"
    PortNumber = 443
    UserName = "access key id"
    Password = "secret access key"
}

Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "s3.log"
$session.Open($sessionOptions)

$remotePath = "/AppMyBucket/Documents";
$localPath = "D:\DocFolder"

$yearFolder =
    $session.ListDirectory($remotePath).Files |
        Where-Object { $_.IsDirectory } |
        Sort-Object -Descending |
        Select-Object -First 1
Write-Host "Latest year is $($yearFolder.Name)"

$yearPath = [WinSCP.RemotePath]::CombinePaths($remotePath, $yearFolder.Name)

$monthFolder =
    $session.ListDirectory($yearPath).Files |
        Where-Object { $_.IsDirectory } |
        Sort-Object -Descending -Property @{ Expression = {
            Get-Date -Date "$($_.Name) 1 $($yearFolder.Name)" -Format "MM" } } |
        Select-Object -First 1

Write-Host "Latest month is $($monthFolder.Name)"

$monthPath = [WinSCP.RemotePath]::CombinePaths($yearPath, $monthFolder.Name)

$dayFolder =
    $session.ListDirectory($monthPath).Files |
        Where-Object { $_.IsDirectory } |
        Sort-Object -Descending |
        Select-Object -First 1

Write-Host "Latest day is $($dayFolder.Name)"

$dayPath = [WinSCP.RemotePath]::CombinePaths($monthPath, $dayFolder.Name)

$latest = 
    $session.ListDirectory($dayPath).Files |
        Where-Object { -Not $_.IsDirectory } |
        Sort-Object LastWriteTime -Descending |
        Select-Object -First 1

Write-Host "Latest file is $($latest.Name), downloading..."

$session.GetFiles($latest.FullName, (Join-Path $localPath "*")).Check()

Write-Host "Done"
$session.Dispose()