I want to display image on UIImageView
using URL and NSString
. I am unable to show image.
My code is:
UIImageView *view_Im
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
}
try this
NSString* combinedString = [stringUrl1 stringByAppendingString stringUrl2];
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...
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.
try AsyncImageView for your this requirement see this example
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.