Swift使用组合字符串从url加载图像

时间:2015-10-05 20:54:43

标签: ios string swift uitableview uiviewcontroller

我有detailTitle,detailDesc字符串的工作代码,但我的detailImage字符串没有显示我的UIImage。我的代码在。

之下

ViewController 这是我的字符串到DetailView控制器

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let subcatVC = self.storyboard?.instantiateViewControllerWithIdentifier("Detail") as! DetailViewController
    subcatVC.detailTitle = self.arrayCategory[indexPath.row][API_PARAM_CAT_NAME] as! String
    subcatVC.detailDesc = self.arrayCategory[indexPath.row][API_PARAM_DESC_NAME] as! String
    subcatVC.detailImage = self.arrayCategory[indexPath.row][API_PARAM_CAT_IMAGE] as! String
    _ = UINavigationController(rootViewController: subcatVC)
    self.navigationController?.pushViewController(subcatVC, animated: false)

    NSLog("%@", self.arrayCategory[indexPath.row][API_PARAM_CAT_IMAGE] as! String);

}

DetailViewController

class DetailViewController: UIViewController {

    var detailTitle:String?
    var detailDesc:String?
    var detailImage:String?




    @IBOutlet weak var textim: UILabel!
    @IBOutlet weak var textbig: UITextView!
    @IBOutlet weak var imagetbig: UIImageView!


override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    textbig.text = detailDesc
    textim.text = detailTitle


    if let url = detailImage {
        load_image(url)
    }


func load_image(urlString:String)
{




    let imgURL: NSURL = NSURL(string: urlString)!
    let request: NSURLRequest = NSURLRequest(URL: imgURL)

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in

        if (error == nil && data != nil)
        {
            func display_image()
            {
                self.imagetbig.image = UIImage(data: data!)

            }

            dispatch_async(dispatch_get_main_queue(), display_image)
        }

    }

    task.resume()
}

我需要;

热门代码运作良好但不显示图片,所以我需要为我的完整图片网址组合字符串。

NSLog("%@",detailImage)  Output =  23423434.jpg

我的基本网址字符串:

let URL_API_HOST2:String = "http://www.bla.com/MyImages/"

我想结合detailImage和URL_API_HOST2

我想显示结合detailImage字符串,如;

http://www.bla.com/MyImages/23423434.jpg

我需要你的帮助。 TY

1 个答案:

答案 0 :(得分:1)

我不确定你的基本url字符串在哪里声明但我不明白为什么你不能简单地连接字符串:

例如,

,而不是:

if let url = detailImage {
    load_image(url)
}

你可以做到

if let url = detailImage {
    load_image(URL_API_HOST2 + url)
}