How to display external image to textview? - ios

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

Related

Unable to set image to the imageview dynamically from web

I'm using the following piece of code to download image from web and add it to array.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: #"http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/02.jpg"]]];
recipePhotos = [[NSMutableArray alloc] initWithObjects:myImage, #"2.png", #"3.png", #"4.png", #"5.png", #"6.png", #"6.png", #"8.png", #"9.png", #"10.png", nil];
});
I'm setting the image on to the ui using the following code,
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
During runtime, image is downloaded from the web is not displayed. Is there anything that I'm missing?
what u are doing is using [UIImage imageNamed: ]
but this will access the object of array recipePhotos and the string in each object will be returned.so u dont have images with those names.
Since the object stored in array is an image u should try this
so try doing
recipeImageView.image = [recipePhotos objectAtIndex:indexPath.row]
or
recipeImageView.image = [UIImage imageWithData:[recipePhotos objectAtIndex:indexPath.row]
In the array recipePhotos only the first index contains proper UIImage as it contains full path that is "http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/02.jpg" (myImage) and other than that only strings without the file path that is http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/
recipePhotos = [[NSMutableArray alloc] initWithObjects:myImage, #"2.png", #"3.png", #"4.png", #"5.png", #"6.png", #"6.png", #"8.png", #"9.png", #"10.png", nil];
});
Try to NSLog the array recipePhotos
Use SDWebImage Class to set image to the imageview dynamically from web
Import following file to your header file.
#import "UIImageView+WebCache.h"
after importing file try following code.
[self.imgLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[self.dictTemp valueForKey:#"camp_image"]]] placeholderImage:[UIImage imageNamed:#"placeholder.jpg"]];
Download library from below link :-
https://github.com/rs/SDWebImage

Getting images from the XML Links

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;
});
});

loop to set UIImages from document directory on viewDidLoad

I have 9 buttons whose images are set by the user dynamically. Each button's current image is saved the user documents folder. In viewDidLoad id like to re-set each image to it's UIButton.
I can do this easily enough with:
NSString *filePath = [self documentsPathForFileName: #"boss1.png"];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[boss1Button setImage:savedBoss forState:UIControlStateNormal];
...... 8 more times....
Of course Id prefer to do it using a loop. Only, Im not sure what that would look like in objective C. In jQuery I could do something like:
$('.bosses').each(function( index ) {
var imageUrl='../images/boss'+(index+1)
$('#'+this.id).css('background-image', 'url(' + imageUrl + ')')
});
How can I create a similar objective-C loop that would increment the boss image name and button name similarly?
Furthermore, is there a better way to do this entirely?
I feel like maybe having an NSArray of image urls and an NSArray of UIButton names and pairing them together using a loop might be better.... but again I'm not sure what the syntax would look like for that here.
Try like this and have button tag like 1 to 8
for (id subview in self.view.subviews) {
if([subview isKindOfClass:[UIButton class]]) {
UIButton* button = (UIButton*)subview;
NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate#"boss%d.png", button.tag]];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[button setImage:savedBoss forState:UIControlStateNormal];
}
}
This will do.
it will be like:
//Add all the buttons in an array for easy loop
NSArray *array = #[btn1,btn2,btn3,btn4... etc];
NSString *name = #"BOSS";
int x = 1;
for(UIButton *btn in array){ // loop through all the buttons
NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:#"%#%d.png",name,x]];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[btn setImage:savedBoss forState:UIControlStateNormal];
x++;
}

Can't see image in my tableView which are coming from JSON

NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
UILabel *detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(250,0,200,50)];
[detailLbl setText:[temp objectForKey:#"distance"]];
[cell.contentView addSubview:detailLbl];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
On the last line i have set the image , but enable to see the image . please help me with this . And i can also not being able to unable segue .
when you loading image from URL but no local,your tableView has drawrect,so when the image finish loading,you must reload tableView or reload the row .
you can use lazy load to get your image. start an request to get image in background,and callback use an block to set image.
this links can help you:
https://github.com/rs/SDWebImage
How do you have define your cell? In some cases using:
initWithStyle:UITableViewCellStyleDefault
instead:
initWithStyle:UITableViewCellStyleValue1
had fixing the issue for me.
Try like this
NSString *imgUrl = #""; //enter your url here
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
UIImage *img = [UIImage imageWithData:data];
cell.imageView.image = img;

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.

Resources