Getting images from the XML Links - ios

http://www.cinejosh.com/news/3/40889/high-court-dismisses-andhra-poris-case.html
In above link I have this tag
<img src="/newsimg/newsmainimg/1433410900_andhra-pori.jpg" alt="High Court Dismisses 'Andhra Pori's Case" class="img-responsive center-block visible-xs">
I have the above tag and I need to get the image on imageview.
My code is like this
NSString *gossipsXpathQueryString = #"//td//img";
//imageGossipsNodes is the array to get data
imageGossipsNodes = [gossipsParser searchWithXPathQuery:gossipsXpathQueryString];
for (TFHppleElement *element in imageGossipsNodes)
{
NSString *imageUrlString = [NSString stringWithFormat:#"http://www.cinejosh.com%#", [element objectForKey:#"src"]];
}
Now I want to pass the string for getting images.How to achieve this?Please help me.
Thanks

You have two options Synchronous and Asynchronous options. Try to use async methods.
UIImage *image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:imageUrlString];
yourImageView.image = image;
or
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imageUrlString];
UIImage *image = [UIImage imageWithData: data];
dispatch_async(dispatch_get_main_queue(), ^{
yourImageView.image = image;
});
});

Related

How to display external image to textview?

I have the following code to display internal image in textview. I am trying to display external http:// link to textview.
The following code works ok. Please help me to display external weblink image.
UITextField *txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)];
txtstate.delegate=self;
NSString * quotStr03 = [[NSString alloc] initWithFormat:#"%#",[dict valueForKey:#"deal-image"]];
txtstate.text=quotStr03;
txtstate.borderStyle = UITextBorderStyleLine;
txtstate.background = [UIImage imageNamed:quotStr03];
[txtstate setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:txtstate];
-(UIImage *) getImageFromURL:(NSURL*) imageURL
{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
by providing url to this function you will get external weblink image

Converting from JSON string to UIImage always gives null?

I Get this kind of JSON:
NOTE: I put dots in "content" because it is too long byte array abd just to explain the situation.
{
"id":"53abc6a7975a9c10c292f670",
"nfcId":"testse",
"company":"TESt",
"qrId":"testvalue",
"address":"ajs;ldfh",
"mimeType":"IMAGE",
"url":"",
"content":"iVBORw0KGgoAAAANSUhEUgAA....."
}
And im trying to get this Json and diplay this information
the "content" field has a Byte array converted on the server from Image to byte array.
I use this code in xCode to convert those bytes to NSData, then to UIImage to be able to display it in UIImageView:
NSData *dataImage = [jsonArray[key] dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"data = %#", dataImage);
UIImage *img = [UIImage imageWithData:dataImage];
NSLog(#"img = %#", img);
The image is always gives me null.Although, data give me array of data.
I tried all kinds of encodings as a NSData parameters also:
dataUsingEncoding:NSASCIIStringEncoding
dataUsingEncoding:NSUTF8StringEncoding
dataUsingEncoding:NSUTF16StringEncoding
dataUsingEncoding:NSUTF32StringEncoding
I've used code like this before
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"data:image/png;base64,%#",jsonArray[key]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imageData];
Note that initWithBase64EncodedString is only available from iOS7 onwards
I tried this code just right now and it works:
NSData* dataImage = [[NSData alloc] initWithBase64EncodedString:jsonArray[key] options:0];
UIImage *img = [UIImage imageWithData:dataImage];
The "content" encoded by Base64 String type.

UIImage with NSData initWithData is nil

The following code does not seem to load an image.
uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:#"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;
datatmp is nil (0x000000) and
the image does exist.
I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:#"newsicon.png"]; instead.
II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):
NSString *iconPath = [[NSBundle mainBundle] pathForResource:#"newsicon" ofType:#"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];
Loading an image from a file is best accomplished with:
[UIImage imageNamed: "newsicon.png"];

How to display an UIImage in a custom cell

I am trying to display an Logo image in my custom cell, from amazon Amazon S3 bucket though StackMob
but its not showing. if l paste the direct url path to the image it works, how do i get around this.
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(2, 2, 67, 67);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
NSURL* imageURL = [NSURL URLWithString:[object valueForKey:#"restoLogo"]];
NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
self.customImage.image = tmpImage;
[cell.contentView addSubview:self.customImage];
the image is at this path [object valueForKey:#"restoLogo"] now returns the s3 url for the data.
If you are not using NSString's instance method
stringByAddingPercentEscapesUsingEncoding:
that could be the issue.
For example
NSString *escapedString = [imgURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Then turn it into a URL and continue on your way.

xcode get black window using storyboard

I'm trying to get a view but, when I get to it in iPhone simulator what I see is a black window and I don't now what I'm doing wrong.
Here's my code:
subview = [[ArticulosViewController alloc]init];
subview.titulo = categoria;
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat: #"http://XXX.XXX.X.XX/XX/%#",[[self.listaArticulos objectAtIndex: [indexPath row]] imagen]]]];
subview.imagen = [UIImage imageWithData: imageData];
[self.navigationController pushViewController:subview animated:YES];
In ArticulosViewControllew I don't have the method -(void)LoadView{}, so I'm quite lost in what I've to do
Thank you all for your help

Resources