快速加载图像从url与组合字符串。

时间:2022-08-20 23:10:39

I have working codes with detailTitle, detailDesc strings but my detailImage string not showing my UIImage. My codes under.

我有详细标题的工作代码,详细描述字符串,但是我的详细描述字符串没有显示UIImage。我的代码。

ViewController Here coming my strings to DetailView Controller

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

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

I need ;

我需要;

Top codes working good but don't show image so i need to combining strings for my full image url.

顶部代码运行良好,但不显示图像,所以我需要为我的完整图像url组合字符串。

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

My base url string :

我的基本url字符串:

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

I want to combining detailImage and URL_API_HOST2

我想结合detailImage和URL_API_HOST2

And I want to show combining detailImage string like;

我想展示的是细线的组合;

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

I need your help. TY

我需要你的帮助。泰

1 个解决方案

#1


1  

I am not sure where your base url string is declared but I don't see why you couldn't simply concatenate the strings:

我不知道你的基本url字符串是在哪里声明的,但是我不明白为什么你不能简单地将这些字符串连接起来:

for example, instead of:

例如,而不是:

if let url = detailImage {
    load_image(url)
}

you can do

你可以做

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

#1


1  

I am not sure where your base url string is declared but I don't see why you couldn't simply concatenate the strings:

我不知道你的基本url字符串是在哪里声明的,但是我不明白为什么你不能简单地将这些字符串连接起来:

for example, instead of:

例如,而不是:

if let url = detailImage {
    load_image(url)
}

you can do

你可以做

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