I have a big problem with very simple code.
I need to get a picture from a Facebook URL and put it on a UIImageView.
I don't know but don't works.
I have tried different links also (no Facebook links also don't works).
-(IBAction)setUserPhoto:(id)sender{
NSURL *url = [url initWithString:#"http://graph.facebook.com/100000769380612/picture?type=large"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
Thank you.
The string has a substitution variable in it, that hasn't been substituted:
http://graph.facebook.com/%#/picture?type=large
You are missing the alias (thats the error i get if i put this url into safari)
Use
NSString *urlString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=large", replacement]; //where replacement is the string that is supposed to go there
NSUrl *url = [NSUrl URLWithString:urlString];
Try the url in the web browser first, to ensure it exists.
You are missing something in url..it contain %# , please replace it with proper string
for eg :
https://graph.facebook.com/DoloresPark/picture?type=large
you URL should be some thing like the above one
-(IBAction)setUserPhoto:(id)sender{
NSString *user = #"DoloresPark";
NSString *urlString = [NSString
stringWithFormat:
#"http://graph.facebook.com/%#/picture?type=large",user];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
have you tried dataWithContentsOfURL:options:error: and reading the &error for any pointers?
The problem is that you are not making a connection to the URL.Use the following -
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:#"Your URL"]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
your class should implement the NSURLConnectionDelegate -
Use the following delegate methods to get the data -
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Hope it helps!
Related
i am trying to fetch an image from URL, but i am not able to do it.
can any one let me know what went wrong
find the code below for your reference.
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
img.image = [UIImage imageWithData:imageData];
where img is a UIImageView
i just create DEMO of Your ImageURL you just Need to put Code Bellow:-
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
ImageURL =[ImageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#"img url ==%#",ImageURL);
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *MYimage = [UIImage imageWithData:data];
NSLog(#"==%#",MYimage);
[imgview setImage:MYimage];
}
Download the Demo i just created of your image URL:-
http://www.sendspace.com/file/m966ae
Thank you :)
Try this.......
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *IMG = [UIImage imageWithData:data];
img.image = IMG;
This is because you have "{" and "}" in your imageURL so you are not able to get your image, so just replace this braces.
-(NSString*)replace:(NSString*)string
{
NSString *str = [[string stringByReplacingOccurrencesOfString:#"{" withString:#"%7B"] stringByReplacingOccurrencesOfString:#"}" withString:#"%7D"];
return str;
}
You have to create your url object in the following way :
NSURL *url = [NSURL URLWithString:[ImageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
then use that url to fetch data. Hope it will work for you.
there are multiple ways to get Image from Url. you can use anyone. but the fastest one that i have tried is
-(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr
{
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
Sorry for my english ^^
I would know how I can have a result from the google converter tool for currency, available on http://www.google.com/finance/converter?a=1&from=USD&to=GBP for example.
I have already tried something like that :
-(IBAction)getResponse:(id) sender{
NSString *myURLString = #"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";
NSURL *myURL = [NSURL URLWithString: myURLString];
NSData *data = [NSData alloc];
data=[NSData dataWithContentsOfURL:myURL];
if(data!=nil && data != ""){
NSString *response = [[[NSString alloc] initWithData:data]]
[label setText: response];
}
else [label setText:#"not working"];
}
But when I click on my button, the label does not have a text (it is like it is empty).
Am I doing something wrong ?
Is it possible what I want to do ?
Thank you very much for your futures answers !
Olivier.
Yeah.. It is possible...
See the data that you are getting is an HTML file or can be treated as xml. Just parse that xml file and get the result. There ia lot of things in that file but you have to only extract
<div id=currency_converter_result>1 USD = <span class=bld>0.6118 GBP</span>
Hey try this URL
http://www.google.com/ig/calculator?hl=en&q=1USD=?INR
This URL will return the JSON
{lhs: "1 U.S. dollar",rhs: "44.5097254 Indian rupees",error: "",icc: true}
Just parse it. :)
Try...
NSData *data = [[NSData alloc] init];
Then check you've got something in response
NSLog(#"%#", response);
Better yet, remove the NSData alloc/init
Replace with
NSData *data=[NSData dataWithContentsOfURL:myURL];
And, you'll need to parse the return result, because you're not going to get back what you expect. Response will contain HTML page.
You better use an asynchronous request to avoid blocking your app. Easy using ASIHTTPRequest:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com/finance/converter?a=1&from=USD&to=GBP"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
[label setText: responseString];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error]; //you can log the error description
[label setText:#"not working"];
}
NSString *myURLString = #"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";
NSURL *myURL = [NSURL URLWithString: myURLString];
NSData *data = [NSData alloc];
data=[NSData dataWithContentsOfURL:myURL];
if(data!=nil){
NSString *response = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSLog(#"%#",response);
}
Try this.
This API is giving HTML responce.
i've managed to incorporate the twitter api into my app and i can call all the data and put it into my app, except for the image.
i've managed to find tuts on how to get the picture if you provide the url, but i want to get the addy from my dictionary to then be put into my uiimageview.
Any help would be appreciated, here is what i have sourced
NSData *imageData =
[[NSData alloc] initWithContentsOfUrl:
[NSString
stringWithContentsOfUrl:
[NSURL URLWithString:#"http://mydomain.com"]
encoding: NSUTF8StringEncoding
error: nil
]
];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[marcaBackground setImage:image];
[imageData release];
[image release];
this is what i have already set up`-(NSString*)author {
return [[contents objectForKey:#"user"] objectForKey:#"screen_name"];
`
NSString *author=[(Tweet*)[auth objectAtIndex:indexPath.row] author];
could someone please advise me on what needs changing for it to work??
NSData *imageData = [[NSData alloc] initWithContentsOfUrl: [NSURL URLWithString:#"http://..."]];
The rest is correct.
I'm having a problem loading a remote image into a UIImageVIew...
It just doesn't show the image, may be i'm missing something...
I also use the described here but with the same results
How to load image from remote server on the UIImageView in iphone?
Can someone help me?
This is the code i'm using
Im getting the data from a xml and on the image element I have the full path
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#", [[promoList objectAtIndex: promoIndex] objectForKey: #"image"]] ]]] ];
With this code the image are displayed correctly
[[detailViewController detailImage] setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: #"http://localhost/promos/preview/1.jpeg"]] ]];
Since the bottom example works as expected and besides the url string the example looks the same the problem should be with getting the url string. The problem is also that the code looks the same but it is quite hard to see. I would refactor the code to something like:
NSObject *urlObject = [[promoList objectAtIndex:promoIndex] objectForKey:#"image"];
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
[[detailViewController detailImage] setImage:image];
Then you can debug the statement properly and make sure that you get what you expect after each step.
Update
To remove newline and tabs you can:
NSString *urlString = [NSString stringWithFormat:#"%#", urlObject];
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
If urlObject already is a string, which it should be, then you can do:
NSString *urlString = [urlObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
which would make it a bit cleaner.
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:#"http://...."]]] retain];
if (img != nil)
{
[imageViewName setImage:img];
[img release];
}
I am trying to set the UIImage of a tableview cell to an absolute url, here's the code i have already:
NSString * imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"images"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//set image
cell.imageView.image = img;
But for some reason this returns null as if the URL supplied is invalid. However i know this is not the case because i can make it work by typing one of the URLs as a string literal into the imageURL pointer and i know the code [[stories objectAtIndex:indexPath.row] objectForKey:#"images"] returns correct URLs as i printed the output to the log and it was fine.
Is there any reason why this would be happening that I'm clearly not seeing here?
You can use the absoluteURL method of NSURL and voila!
NSURL *url = [[NSURL URLWithString:imageURL] absoluteURL];