Xcode tableview cell unable display image via url - ios

I followed the code based on here tableview Tutorial and i did successfully show something in my tableview.
I want to change the cell image by using url link from website.
Here is my code:
NSString *str = [NSString stringWithFormat:#"http://piq.codeus.net/static/media/userpics/piq_126382_400x400.png"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
If I change my str to
NSString *str = #"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSrKOobbDUVMwrrqMkpvfhITeyQewM8Kj0fZg_X_u3TaM-CLi5QfA";
then it will successfully change the image of each cell in table view.
Both urls link to the same image but the first link image size is bigger than second link.

Problem solved. Thanks to #GoodSp33d.
For those who are using Xcode 7. Please refer to this solution.
Here is my updated code:
NSString *str = [NSString stringWithFormat:#"your image url"];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL : url];
cell.imageView.image = [UIImage imageWithData:data];

Related

uiimageview is not Working using URLWithString:

_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#".../images/Profile/Pratik Maniya Photo.jpg"]]];
I want to display image using the URL link in the code,
but there is not any result.
The problem is in the spaces symbols in url.
You can use:
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]
Full code:
NSString *base_url = #"https://safesocpgm.000webhostapp.com/images/Profile/Pratik Maniya Photo.jpg";
NSString* encoded_url = [base_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:encoded_url]]];

Unable to create NSURL with special characters (éè) with Objective C

I am unable to load an image because of nil returned by NSURL. The url looks like this : http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg. When I enter my URL on firefox, I get my image but when I try in firefox the encoder http://www.example.fr/wp-content/uploads/2014/06/pr%3Flinaire.jpg I have no image...
Here is the code I use to load my image :
NSString *myString = #"http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg";
NSURL *url = [NSURL URLWithString:myString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
That returns nil for url, so everything after is nil..
Thank you for your help guys
That's because those characters are not valid in a URL. Escape them first:
NSString *escaped = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escaped];
// etc.

Getting an image fails to create NSURL

Im trying to get an image from a url link.
NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
NSLog(#"thmbnail:%#",[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
NSLog(#"movimgdata:%#",data);
UIImage *img = [[UIImage alloc]initWithData:data];
NSLog(#"movimg:%#",data);
NSData *imgData = UIImagePNGRepresentation(img);
when i log thmbnail i get this "ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png"
Note:i didn't put the original ip.
I get null when i log data and of course img will also be null. I really dont know why I'm getting null. because when I tried entering the url in a browser it displays the image. I used the same code in another part of my app and it works but when i used it in this it fails. Is it because of the spaces between "Our_Logo_480p"?
you might need to use
NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
yes it's because of spaces, use
NSString *myUrl = [NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

UITableViewCell images not loading ios

I am trying to download images asynchronously and display them in UITableViewCell imageView. The problem is that my NSURL is nil for some reason..
NSString *urlString = feed[#"imageURL"];
NSLog(#"urlString %#", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"url image %#", url);
This will print something like
urlString http://www.....image.jpg
url image (null)
Can someone please tell me why this is happening? Thanks.
What your doing is getting the contents within a url with NSURL. Once you have the contents of the url, you are basically saying to the compiler, load these SOMETHING contents from this url. But that SOMETHING whether it's an image or json object or whatever, needs to be loaded into an NSData object then load the NSData object into a UIImage object. Now you can just say cell.imageView.image = theUIimage for your exact problem but heres a simple example that just loads such UIImage into a UIImageView like so:
NSURL *URL = [NSURL URLWithString:#"http://l.yimg.com/a/i/us/we/52/37.gif"];
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.image = img;
[self.view addSubview:imageView];
Please no downvote :)

NSURL with Pipes |

I'm trying to initialize an UIImage via URL.
This is the code I wrote:
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];
....
An example map_url looks like the following:
https://maps.google.com/maps/api/staticmap?size=200x200&maptype=roadmap&markers=size:big|color:green|72.3601030185,41.4626763775&markers=size:big|color:red|45.4563468516,94.4774796973&sensor=false
It seems that the NSURL Object cant handle the | Pipe Character the right way because i dont get any image data. If I delete the parameters separated by the pipe out of the url everything works fine.
Any Ideas how to solve this problem?
ok i found a solution for this. Encoding is what did go wrong.
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];

Resources