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

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;

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

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

Load images from a documents subdirectory into UICollectionViewCell

I have several images in png format in "/Documents/saved/" directory.
I loaded this directory into an array called "contents"
When i use NSLog to view the contents of this array, i see the images as well as a .DS_STORE file
What i want to do is load all these images into UICollectionView cells.
I tried this,
[[cell collectionImageView] setImage:
[UIImage imageWithContentsOfFile:
[contents objectAtIndex:indexPath.item]]];
The collectionImageView is connected to the UIImageView in the UICollectionViewCell
But it doesn't seem to work.EDIT
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/DImage"];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:NULL];
NSLog(#"%#", contents);
This gives me the output
2013-03-25 10:37:20.186 aUX[543:c07] (
".DS_Store",
"savedImage_0.png",
"savedImage_1.png",
"savedImage_2.png",
"savedImage_3.png",
"savedImage_4.png",
"savedImage_5.png"
)
(I have dragged a UIImageView named collectionImageView.)
I need to display these images one by one in the collection view cells.
It is not clear what data types you are using. Maybe breaking up your one-liner might help bring more clarity to the situation. Also, in this way you can check each step with NSLog or in the debugger.
NSString *fileName = [contents objectAtIndex:indexPath.item];
NSString *path = [filesDirectory stringByAppendingPathComponent:fileName];
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *cellImageView = cell.collectionImageView;
cellImageView.image = image;
Maybe your contents array contains NSURLs rather than NSStrings. In this way you can find out.
in ur cellForRowAtIndexPath
cell.collectionImage.image=[contents objectAtIndex:indexPath.row];
return cell;

UIImage is empty after calling imageWithContentsOfFile

As you can see, that I have put my the national flags in a folder in Xcode and I am trying to display it to the navigation bar. However, it is not showing up and I found out:
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
image is "nil".
Any idea? Thanks!
You could use like this
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageNamed:imageName];
Please Try This
Check whether the file actually exists. I suspect it doesn't. Use [NSFileManager defaultManager] fileExistsAtPath:.
Where was the image path you are sending NSString to here
UIImage imageWithContentsOfFile:imageName
send the path to that method. or make like this
UIImage *image = [UIImage imageNamed:[NSString stringwithFormat:#"%#.icns",countryName]];

iOS Split view controller - changing image in detailViewController

This is another of those "I'm sure there's an easy answer to this" questions, but I find myself baffled.
I'm using the split view controller from the template. I'm successfully passing a NSString to the _detailItem variable, but am unable to use it to create and load an image into an UIImageView (named "stripImage").
It works up until:
[self.stripImage setImage:[UIImage imageNamed: _detailItem]];
If I put a string literal into the same line of code (like #"image20.jpg"), it works fine.
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
NSLog(#"_detailItem is still: %#",_detailItem);
// correctly reports the name of the imagefile (for example: "image20.jpg"
[self.stripImage setImage:[UIImage imageNamed: _detailItem]];
NSLog(#"The image being shown is: %#",self.stripImage.image);
//returns "The image being shown is: (null)"
}
}
Please help keep my head from exploding. Thanks in advance.
... and I've tried it this way:
(in my Master view controller):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected row %i",[indexPath row]);
NSString *imageName = [[stories objectAtIndex:[indexPath row]] objectForKey:#"link"];
NSLog(#"The image is: %#", imageName);
// These two work, oddly enough...
self.detailViewController.detailTitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:#"title"];
self.detailViewController.detailSubtitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:#"subtitle"];
// this one, not so much...
[self.detailViewController loadUpImageWith:imageName];
}
and in my Detail view controller:
- (void)loadUpImageWith:(NSString *)what
{
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:#"strips/%#", what]];
NSLog(#"The file's url is: %#",path);
UIImage *img = [UIImage imageWithContentsOfFile:path];
NSLog(#"The resultant image is: %#",img);
stripImage.image = img;
}
But here's the weird thing... If I replace the variable ("what" in this case) with a string literal:
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:#"strips/%#", #"grumbles300.jpg"]];
... it works, displaying that one image. But I want to be able to use a variable there!!
Help me Obiwan Kenobi! You're my only hope.
Embarrassing operator error.
I was adding a buncha images to the bundle, but it apparently kept track of the folder that contained them, which needed to be added to the file name. I thought UIImage imageNamed: would track down the images, as long as they were contained in the main bundle. Apparently not.
Perhaps this would be useful to someone who's experiencing the same sort of brain fart.
After resolving this, it was easy to do this:
- (void)loadupDetailTitle:(NSString *)title
subtitle:(NSString *)subtitle
image:(NSString *)imageName
{
NSString *path = [#"strips/" stringByAppendingPathComponent:imageName];
stripImage.image = [UIImage imageNamed:path];
detailTitle.text = title;
detailSubtitle.text = subtitle;
}

Resources