How to display image on UIImageView using URL and NSString in ios?

后端 未结 10 1956
孤城傲影
孤城傲影 2021-01-02 16:49

I want to display image on UIImageView using URL and NSString. I am unable to show image.

My code is:

UIImageView *view_Im         


        
相关标签:
10条回答
  • 2021-01-02 17:00

    For Swift 3.0

    class ImageLoader {
    
    
    var cache = NSCache<AnyObject, AnyObject>()
    
    class var sharedLoader : ImageLoader {
        struct Static {
            static let instance : ImageLoader = ImageLoader()
        }
        return Static.instance
    }
    
    func imageForUrl(urlString: String, completionHandler:@escaping (_ image: UIImage?, _ url: String) -> ()) {
    
        DispatchQueue.global().async {
            let data: NSData? = self.cache.object(forKey: urlString as AnyObject) as? NSData
    
            if let goodData = data {
                let image = UIImage(data: goodData as Data)
                DispatchQueue.main.async {
                    completionHandler(image, urlString)
                }
                return
            }
    
            let url:NSURL = NSURL(string: urlString)!
            let task = URLSession.shared.dataTask(with: url as URL) {
                data, response, error in
                if (error != nil) {
                    print(error?.localizedDescription)
                    completionHandler(nil, urlString)
                    return
                }
    
                if data != nil {
                    let image = UIImage(data: data!)
                    self.cache.setObject(data as AnyObject, forKey: urlString as AnyObject)
                    DispatchQueue.main.async {
                        completionHandler(image, urlString)
                    }
                    return
                }
            }
            task.resume()
        }
    
    }
    }
    

    Usage

    ImageLoader.sharedLoader.imageForUrl(urlString: imageUrl as! String) { (image, url) in
                self.imageView.image = image
    }
    
    0 讨论(0)
  • 2021-01-02 17:01

    try this

    NSString* combinedString = [stringUrl1 stringByAppendingString stringUrl2];
    
    0 讨论(0)
  • 2021-01-02 17:04

    for showing image from url you can try this...

     [ImageViewname setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",your url]]];
    

    for showing image locally from app you can try this....

     ImageViewname.image = [UIImage imageNamed:@"test.png"];
    

    I hope this will help you.

    happy coding...

    0 讨论(0)
  • 2021-01-02 17:08
        NSString *url_Img1 = @"http://opensum.in/app_test_f1";
        NSString *url_Img2 = @"45djx96.jpg";
    
        NSString *url_Img_FULL = [url_Img1 stringByAppendingPathComponent:url_Img2];
    
        NSLog(@"Show url_Img_FULL: %@",url_Img_FULL);
        view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]; 
    

    try this.

    0 讨论(0)
  • 2021-01-02 17:16

    try AsyncImageView for your this requirement see this example

    1. AsyncImageView
    0 讨论(0)
  • 2021-01-02 17:16

    I tested your url and found the issue.

    The issue is with this line:

    NSString *url_Img1 = @"http://opensum.in/app_test_f1/;";
    

    You are adding a ; at last of the url string.

    So when you concatinate two strings it'll look like: http://opensum.in/app_test_f1/;45djx96.jpg

    That is not a valid url.

    The correct url is : http://opensum.in/app_test_f1/45djx96.jpg

    Change the code like:

     NSString *url_Img1 = @"http://opensum.in/app_test_f1/";
    

    It'll work.

    0 讨论(0)
提交回复
热议问题