Comment afficher une image sur UIImageView en utilisant l'URL et NSSsortingng dans ios?

Je veux afficher l'image sur UIImageView utilisant l'URL et NSSsortingng . Je suis incapable de montrer l'image.

Mon code est:

 UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(view_Image_Pos_X, view_Image_Pos_Y, 179, 245)]; view_Image.backgroundColor = [UIColor greenColor]; view_Image.tag = img; view_Image.userInteractionEnabled=YES; view_Image.autoresizesSubviews = YES; view_Image.alpha = 0.93; [self.view addSubview:view_Image]; 

Voici ce que j'essaie:

  if (img == 0) { NSSsortingng *url_Img1 = @"http://opensum.in/app_test_f1/;"; NSSsortingng *url_Img2 = @"45djx96.jpg"; NSSsortingng *url_Img_FULL = [NSSsortingng ssortingngWithFormat:@"%@%@", url_Img1,url_Img2]; NSLog(@"Show url_Img_FULL: %@",url_Img_FULL); NSURL *url_img = [NSURL URLWithSsortingng:url_Img_FULL]; NSLog(@"Show: url_img %@",url_img); // Here below line working perfectly and image is showing view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:@"http://opensum.in/app_test_f1/45djx96.jpg"]]]; // working code 

mais je ne veux pas ce que je veux concataner deux url faire un url (ie, url_Img_FULL), puis passer à une image comme ci-dessous:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:url_Img_FULL]]]; // Not Working

Je veux de "url_Img_FULL" (ie: combinaison de deux url) L'image montrera. Vous pouvez également vérifier que l'URL fonctionne correctement. Une idée?

  NSSsortingng *url_Img1 = @"http://opensum.in/app_test_f1"; NSSsortingng *url_Img2 = @"45djx96.jpg"; NSSsortingng *url_Img_FULL = [url_Img1 ssortingngByAppendingPathComponent:url_Img2]; NSLog(@"Show url_Img_FULL: %@",url_Img_FULL); view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:url_Img_FULL]]]; 

essaye ça.

Fais-le

 NSSsortingng *imgURL = @"imagUrl"; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:imgURL]]; [YourImgView setImage:[UIImage imageWithData:data]]; 

Utiliser GCD: Si vous ne voulez pas accrocher votre application, vous pouvez download votre image en arrière-plan.

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSSsortingng *imgURL = @"imagUrl"; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:imgURL]]; //set your image on main thread. dispatch_async(dispatch_get_main_queue(), ^{ [YourImgView setImage:[UIImage imageWithData:data]]; }); }); 

essaye ça

 NSSsortingng* combinedSsortingng = [ssortingngUrl1 ssortingngByAppendingSsortingng ssortingngUrl2]; 

Obtenir l'image du code suivant

 NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:@"Your image URL Here"]]; 

Ensuite, définissez l'image en utilisant le code suivant

 [UIImage imageWithData:imageUrl]; 

Pour Swift 3.0

Synchrone:

 if let filePath = Bundle.main().pathForResource("imageName", ofType: "jpg"), image = UIImage(contentsOfFile: filePath) { imageView.contentMode = .scaleAspectFit imageView.image = image } 

Asynchrone:

Créez une méthode avec un gestionnaire d'achèvement pour get datatables d'image de votre URL

 func getDataFromUrl(url:URL, completion: ((data: Data?, response: URLResponse?, error: NSError? ) -> Void)) { URLSession.shared().dataTask(with: url) { (data, response, error) in completion(data: data, response: response, error: error) }.resume() } 

Créer une méthode pour download l'image (démarrer la tâche)

 func downloadImage(url: URL){ print("Download Started") getDataFromUrl(url: url) { (data, response, error) in DispatchQueue.main.sync() { () -> Void in guard let data = data where error == nil else { return } print(response?.suggestedFilename ?? url.lastPathComponent ?? "") print("Download Finished") self.imageView.image = UIImage(data: data) } } } 

Usage:

 override func viewDidLoad() { super.viewDidLoad() print("Begin of code") if let checkedUrl = URL(ssortingng: "http://img.iosberry.com/iphone/og.png") { imageView.contentMode = .scaleAspectFit downloadImage(url: checkedUrl) } print("End of code. The image will continue downloading in the background and it will be loaded when finished.") } 

Extension:

 extension UIImageView { func downloadedFrom(link: Ssortingng, contentMode mode: UIViewContentMode = .scaleAspectFit) { guard let url = URL(ssortingng: link) else { return } contentMode = mode URLSession.shared().dataTask(with: url) { (data, response, error) in guard let httpURLResponse = response as? HTTPURLResponse where httpURLResponse.statusCode == 200, let mimeType = response?.mimeType where mimeType.hasPrefix("image"), let data = data where error == nil, let image = UIImage(data: data) else { return } DispatchQueue.main.sync() { () -> Void in self.image = image } }.resume() } } 

Usage:

 override func viewDidLoad() { super.viewDidLoad() print("Begin of code") imageView.downloadedFrom(link: "http://img.iosberry.com/iphone/og.png") print("End of code. The image will continue downloading in the background and it will be loaded when finished.") } 

Pour 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(urlSsortingng: Ssortingng, completionHandler:@escaping (_ image: UIImage?, _ url: Ssortingng) -> ()) { DispatchQueue.global().async { let data: NSData? = self.cache.object(forKey: urlSsortingng as AnyObject) as? NSData if let goodData = data { let image = UIImage(data: goodData as Data) DispatchQueue.main.async { completionHandler(image, urlSsortingng) } return } let url:NSURL = NSURL(ssortingng: urlSsortingng)! let task = URLSession.shared.dataTask(with: url as URL) { data, response, error in if (error != nil) { print(error?.localizedDescription) completionHandler(nil, urlSsortingng) return } if data != nil { let image = UIImage(data: data!) self.cache.setObject(data as AnyObject, forKey: urlSsortingng as AnyObject) DispatchQueue.main.async { completionHandler(image, urlSsortingng) } return } } task.resume() } } } 

Usage

 ImageLoader.sharedLoader.imageForUrl(urlSsortingng: imageUrl as! Ssortingng) { (image, url) in self.imageView.image = image } 
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSSsortingng *str = self.recordlarge[@"images"]; NSLog(@"Project Gallery id Record : %@",str); NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithSsortingng:str]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.myimagelarge setImage:[UIImage imageWithData:data]]; }); }); 

essayez AsyncImageView pour votre cette exigence voir cet exemple

  1. AsyncImageView

J'ai testé votre URL et trouvé le problème.

Le problème est avec cette ligne:

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

Vous ajoutez un ; enfin de la string d'URL.

Donc quand vous concatirez deux strings, ça ressemblera à: http://img.iosberry.com/iphone/;45djx96.jpg

Ce n'est pas une URL valide.

L'URL correcte est: http://img.iosberry.com/iphone/45djx96.jpg

Changez le code comme:

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

Ça va marcher.

pour montrer l'image de l'URL, vous pouvez essayer cela …

  [ImageViewname setImageWithURL:[NSURL URLWithSsortingng:[NSSsortingng ssortingngWithFormat:@"%@",your url]]]; 

pour montrer l'image localement de l'application, vous pouvez essayer ceci ….

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

J'espère que cela t'aidera.

encoding heureux …