Am having some difficulty getting an Image from a url, and then displaying it in an image well in the interface.
This is the code I'm currently using, it doesn't work and compiles with no errors:
NSURL *url = [NSURL URLWithString:#"http://www.nataliedee.com/061105/hey-whale.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[imageView setImage:[NSImage imageNamed:newIMAGE]];
Any ideas as to what is wrong here?
You are passing in image data as a string to a method that is trying to use that string as the name of the image, which of course doesn't exist.
What you need to do is create an NSData object from your URL dataWithContentsOfURL:, once you have the NSData object use this to create the UIImage via imageWithData:.
Related
I have an NSData object and I want to display it in a WebView. Can WebView handle/display NSData objects?
NSData *data = ... // some data I've gotten from NSURLConnection
WebView *webView = ... [[WebView alloc] init];
Yes!
NSData is handled by UIWebView when properly supplied with MIME type.
This snippet loads a .docx file in a WebView
NSString *path = [urlFileInView path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
webViewForDocsView.delegate = self;
[webViewForDocsView loadData:data MIMEType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:#"UTF-8" baseURL:nil];
Without the Content-Type header, the web view has no way to know how to interpret the data. That's why the Content-Type header was added.
If for some reason you want an NSData capture of network traffic that you can replay whenever you want, consider writing a custom NSURLProtocol. If you write a handler for http then it will override the built-in one; give it a fall-through memory and it can redirect those requests to the real one.
Do it like this
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
[self.webView loadHTMLString: newStr baseURL:nil];
I am new to iPhone App development.
When I run a sample project, I did which parses an xml feed and displays the contents along with image in a table view, I get this error -
"NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x712e450'"
It is occurring only when I try to display the image in UITableViewCell.
The code I used for getting images from url is -
if([elementName isEqualToString:IMAGE])
{
NSURL *imageUrl = [attributeDict objectForKey:#"url"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}
where bbc is a class(NSObject subclass) object used to store the parsed contents.
I think you are using NSString as NSURL. Try this:
NSURL *imageUrl =[NSURL URLWithString:[attributeDict objectForKey:#"url"]];
It looks like "url" is in fact an NSString, not an NSURL object. Convert it to an NSURL object yourself:
if ([elementName isEqualToString:IMAGE])
{
NSString *urlStr = [attributeDict objectForKey:#"url"];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}
imageURL is not a NSURL, but a string.
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:post.imageURL]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
I have this code and imgData always gets nil. I checked post.imageURL and it returns a string which in a url format. I also replaced the string with a constant string(URL format) that I hardcoded but imgData still got null. This code runs on the background. Do you guys see any problem?
Your code looks good, are you sure your URL is valid ?
If post.imageURL is not valid, thus URLWithString: will be null and also your data.
I have tried with "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" to be sure, and it worked, so I guess the problem come from your URL.
Split your code :
NSURL *url = [NSURL URLWithString:#"http://www.google.com/intl/en_ALL/images/srpr/logo1w.png"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
And check the value of each of the variable.
following problem:
I created a NSXMLParser subclass to get Xbox Live profile data, the class works fine but i have a problem with displaying an image.
NSLog(#"%#", [[xbl.games objectAtIndex: i] objectForKey: #"Image64Url"]);
output:
http://tiles.xbox.com/tiles/+4/R1/1Wdsb2JhbC9ECgUAGwEfV1oiL2ljb24vMC84MDAwAAAAAAAAAPpahOQ=.jpg
displaying the image:
NSURL *url = [NSURL URLWithString:[[xbl.games objectAtIndex: i] objectForKey: #"Image64Url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
gamerPicView.image = [[UIImage alloc] initWithData:data];
Does not work, but if i run:
NSURL *url = [NSURL URLWithString:#"http://tiles.xbox.com/tiles/+4/R1/1Wdsb2JhbC9ECgUAGwEfV1oiL2ljb24vMC84MDAwAAAAAAAAAPpahOQ=.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
gamerPicView.image = [[UIImage alloc] initWithData:data];
Its working, im really confused whats my mistake?
Thanks for help and greetings from switzerland.
I would print what 'url' is after your first (unsuccessful) attempt at displaying the image and see if it is the same URL or not as the code that works, since this seems like the only place something could be wrong.
Edit: And if you still can't figure out the problem, post the NSLog output for 'url' in both cases.
I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html