I've gone through a lot of code from the web in an attempt to load an image view via URL. Unfortunately, none of the solutions I tried has worked for me and I get very strange errors. I am getting image data, but no image from this NSData. I used the below code with a sample URL and it works, giving me the represented image. However when I switch it to use my actual URL I run into problems because it seems like it is attempting to create/return an image which is too large in size. Here is my code:
NSString *urlString = [[[_xmlDictionary objectForKey:@"response"] objectForKey:@"movie"] objectForKey:@"poster"];
NSArray *parts = [urlString componentsSeparatedByString:开发者_StackOverflow社区@"\""];
urlString = [parts objectAtIndex:1];
urlString = [urlString stringByAppendingFormat:@"/"];
urlString = [urlString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSURL * imageURL = [NSURL URLWithString:urlString];
NSLog(@"URL:%@",urlString);
NSURL * imageURL = [NSURL URLWithString:urlString];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage * image = [[UIImage alloc] initWithData:imageData];
m_imageView = [[UIImageView alloc] initWithImage:image];
m_imageView.frame = CGRectMake(10, 10,300,300);
[self.view addSubview:m_imageView];
[imageData release];
[image release];
I'd appreciate any help, thanks in advance.
You can convert your response from your URL to NSData and then use
NSURL *url = [NSURL URLWithString:@"<YOUR URL STRING HERE>"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
yourImageView.image = tmpImage;
Hope this helps.
EDIT:
Also to make it work in background you should call it using a seperate thread in an asynchronous manner so that it wont block your main thread as shown below.
[NSThread detachNewThreadSelector:@selector(downloadAndLoadImage) toTarget:self withObject:nil];
For large amounts of data you should consider using NSURLConnection:
NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection * conn = [NSURLConnection connectionWithRequest:request delegate:myDelegate];
This way you can keep track of the data returned from the server with:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
You have stated that I have used below code with different url and it is giving image
then it would be helpful if you provided the URL that you are trying to use.
Another idea to help track down the issue would be to UIWebView to verify that you are able to load the required image.
Please try the following code. i have tested it. its working fine.
//in .h file
IBOutlet UIImageView *imgTest;
-(IBAction)buttonTapped:(id)sender;
-(void)LoadImage:(NSString *) irlString;
-(void)setImage:(NSData *) imgData;
//in .m file write the following code:
-(IBAction)buttonTapped:(id)sender
{
[self performSelectorOnMainThread:@selector(LoadImage:) withObject:@"http://www.google.com/images/errors/logo_sm.gif" waitUntilDone:NO];
}
-(void)LoadImage:(NSString *) urlString
{
NSURL *imgURL=[NSURL URLWithString:urlString];
NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
[self performSelectorInBackground:@selector(setImage:) withObject:imgData];
}
-(void)setImage:(NSData *) imgData;
{
imgTest.image=[UIImage imageWithData:imgData];
}
NSString *img=[NSString stringWithFormat:@"https://i1.sndcdn.com/avatars-000098290475-iw9m74-large.jpg?e76cf77"];
NSURL *url = [NSURL URLWithString:img];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img1 = [[UIImage alloc]initWithData:data];
self.artWorksImage.image = img1;
THIS WILL HELP YOU GET THE IMAGE AND THE PARAGRAPH AND TITLE FROM YOUR WEBSITE!
If you want me to help you or get the second image or any help you need type in comment I will re write my code.
This is a answer quite late but I think it still might help in your future. You can go into parsing the website and that is the right way to do it but I will show you how to do it a different way, this can also be used to read xml, html, .com, anything and also, .rss so it can read RSS Feeds.
Here :
This can get your first paragraph, if you request I will show you how to get the second paragraph and so on. So if you want to get money change your scanner to find $
Title would be name but if it is not tell me I will show you how to get second title or header. See this and down. I will show you how to get the html or xml from the whole website so you can see the whole code of the website and look for the about so read the website first and then read the code and you see the dogs name in the code find what it is under title tag or under header and change the code underneath to your needs read this whole post to understand everything. This below is main code of how to get info. Go to second code block to find out how to get code of your website in your Log or Console. To find your image same thing look through the NSLog and look for any image links like www.example.com/images/image.jpg or image.png or image.gif or some image extension and look at what code is initiating this like is it or there are literally 100's of image codes in html and xml, look for the url look at the code and then use it in your scanner change the
to your code and then make sure only the url of the image is still there like keep shortening it until you have nothing except url.
So you have
you want : www.example.com/images/image.jpg
You would use the scanner below and change the
to <media:content url:\"
so scan it until it is static so if you know that that part will always be the same then scan it up to there and then the second part of the scanner end it with \"
See the first code block underneath the big one? This is the part we need to change -
NSString *webString2222 = mastaString;
NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];
NSString *content2222 = [[NSString alloc] init];
//Change <p> to suit your need like <description> or <h1>
[stringScanner2222 scanUpToString:@"<p>" intoString:Nil];
[stringScanner2222 scanUpToString:@"." intoString:&content2222];
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
description.text = filteredTitle;
In our case we change this part to -
[stringScanner2222 scanUpToString:@"<media:content url:\"" intoString:Nil];
[stringScanner2222 scanUpToString:@"\">" intoString:&content2222];
Now it has the url but the first part of the scanner always stays so use -
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"
Then use this to download the image -
NSURL * imageURL = [NSURL URLWithString:[filteredTitle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]])];
UIImage * image = [UIImage imageWithData:imageData];
[imageView setImage:image];
//This is your URL NSURL *URL = [NSURL URLWithString:@"URL HERE"]; //This is the data your pulling (dont change) NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
{
NSString *webString2222 = mastaString;
NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];
NSString *content2222 = [[NSString alloc] init];
//Change <p> to suit your need like <description> or <h1>
[stringScanner2222 scanUpToString:@"<p>" intoString:Nil];
[stringScanner2222 scanUpToString:@"." intoString:&content2222];
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
description.text = filteredTitle;
}
Title ? Same deal change the <p>
to a <title>
in RSS <description>
and <title>
.
Image ? Same deal change the <p> to what ever your RSS or website uses to get a image to find
But remember for both of them when you change the
` you see the link which says stringByReplacingOccurences of you have to change that as well.
out then you have to delete this and make your code like this :
//This is your URL
NSURL *URL = [NSURL URLWithString:@"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
NLog(@"%@", string)
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
Now check your log it shows your whole website html or rss code then you scroll and read it and find your image link and check the code before it and change the String Scanner to your needs which is quite awesome and you have to change the stringByReplacingOccurences of.
Like I said images are a bit tricky when you do it with this method but XML Parsing is a lot easier ONCE you learn it , lol. If you request I will show you how to do it. Tell me if your having problems looking through the html or xml and specify which website again and I will look through and I will tell you how to get everything you want. Make sure :
If you want me to show you how to do it in XML just comment. If you want me to show you how to find the second paragraph or image or title or something just comment.
IF YOU NEED ANYTHING JUST COMMENT.
Bye have fun with code I provided, anything wrong JUST COMMENT! !!!!
:D
精彩评论