iphone sdk how to add uitableview cell image dynamically? - iphone

I am new to this iPhone development. Am developing a catalogue application. In that i need to give all product details with product images as tableview cell image dynamically since my images are coming from web service. I did this,
NSString* imageName = [NSString stringWithFormat:#"%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]];
cell.imageView.image.size.width==20;
cell.imageView.image.size.height==20;
cell.imageView.image = [UIImage imageNamed:imageName];
NSLog(#"imagename:%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]);
Am getting all the images in my console window and i checked that using nslog. But am not getting the images with simulator.What am doing wrong in this? Can anyone of you help me out?
Thanks in advance.

UIImage imageNamed: takes an NSString so if you want to use a formatted string like that use NSString's stringWithFormat
try:
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",currentLink]];
As you are new to iPhone development I'll just expand that out to make it clearer (Ignore this if you already got what was happening there)
NSString* imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];

Do as follows:
NSString *imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];

Apple documentation contains an example of putting downloaded images into table cells. It's called LazyTableImages and would probably help with what you're trying to do.

Related

how to show images at cell.imageview.image from NSArray?

Image Array=[NSArray arrayWithObjects:[UIImage imageNamed:#"1 (1).gif"],
[UIImage imageNamed:#"1 (2).gif"],[UIImage imageNamed:#"1 (3).gif"],[UIImage imageNamed:#"1 (4).gif"],[UIImage imageNamed:#"1 (5).gif"],[UIImage imageNamed:#"1 (6).gif"],[UIImage imageNamed:#"1 (7).gif"],nil];
//here is another array of objects showing in table view
alphabetsArray =[[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",nil];
//everything working fine until when i add this code,app crashes with SIGABRT
cell.imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
Actually i need to ask some more things:
Is there any problem if number of images in array isn't equal to number of object in tableview?
Is there any other way to add images if they are huge in number except of uiimage imageNamed:"whatever.jpeg"
What if add a folder and give its address or name and it picks up images by itself..
What is most reasonable method of adding images in project if they are 250 in number(i-e sqlite,coreData,hardcode in project as i did)?
First, there is issue with this line [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]].
The [imageArray objectAtIndex:indexPath.row] is returning UIImage object in your current implementation, and you are trying to get UIImage with name of UIImage object.
You should just return the UIImage object directly to the cell.imageView.image.
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
Second, I am not sure how many rows are there in the table, but the method objectAtIndex can throw exception if the index is more than the number of elements in the array, causing app crash.
1) There would be no problem with this, unless you want to have the same number. It won't throw an error, but your app logic will not work properly.
2) Not that i know of, It's easy to do this though, use an easy naming convention such as image01, That way you can just copy and paste the code and change the ending numbers of the image
3) Im not sure of what you are asking.
4) Typically you would just need to add the images to your project via drag-N'-drop or by picking the files. Core data and sqlite are for the actual app that you are developing, not to store images in Xcode.
As for the main question, You say you want to show the images from an NSArray, if thats the case, just get the index path of the tableview, and look up the corresponding image using objectAtIndex:
Hope that helps a bit :)
Actually i have figured something out , there is an easy way to do.
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif", [TextShownInTableArray objectAtIndex:indexPath.row]]];
i have alphabetics in this array, and i wanted to use different images with each of the alphabet, so just added images in project with same name as alphabets had. it worked fine with this line of code. TextShownInTableArray is your array with text :)
Image Array=[NSArray alloc]initWithObjects:[UIImage imageNamed:#"8.gif"],
[UIImage imageNamed:#"2.gif"],[UIImage imageNamed:#"3.gif"],[UIImage imageNamed:#"6.gif"],[UIImage imageNamed:#"5.gif"],[UIImage imageNamed:#"4.gif"],[UIImage imageNamed:#"7.gif"],nil];
your array alloc and that array 'retain' this array and try this code .thanks
[imageArray retain];
only change in your code this line . and solve your problem
cell.imageView.image=[imageArray objectAtIndex:indexPath.row];
i hope this code useful for You . Check this
cell.imageView.frame = CGRectMake(5, 5, 50, 50);
NSString *imgstr = [[Arr objectAtIndex:indexPath.row] objectForKey:#"thumb"];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgstr]]];

Populating Data from NSMutableArray in iPhone

I have three arrays and I have copied all these arrays into a single array. All these three arrays are arrays of dictionaries.All arrays has a field called picture, but that pictures is coming from different sources- URL in one array, data in other and files in the third one.
Say, Array1 has dictionaries with a key - picture and its loaded from NSURL.
Similarly, Array2 and Array3 has dictionaries with same key name - picture and loaded from ContentofFiles and NSData.
Now, I want to populate tableview, of course,m having Custom UITableViewCell, it has image view as its content view. To load that image, what should I do.
I was doing this thing..
NSURL *url = [NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]];
cell.contactImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
But, this will crash if cell.contactImageView.image don’t receive image from NSURL.So, what should I do? Any help, will be appreciated
But,
all you is to check if the received image is null and if it is, then set a template photo image called no photo like a photo on facebook when no profile picture is selected
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
if (img)
cell.contactImageView.image = img;
else
cell.contactImageView.image = [UIImage imageNamed:#"no_photo.png"];
If these images are retrieved from the net, i would suggest not using [NSData dataWithContentsOfURL:].
You should be using a non blocking method, that loads images asynchronously. Using this method on numerous rows in a table will cause performance issues.
Here's my recommendation , use the SDWebImage Library . Easy to use, and even easier to install.
Once you add the library to your project, simply #import the UIImageView+WebCache.h class usage example is below.
[cell.contactImageView.image setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can add one more key in dictionary which will specify from where to take photo and accordingly you can provide image to cell.contactImageView, if you want to provide image from different sources.
Thanks a lot buddies for your Quick reply,especially #skram, due to your suggestion,my tableview performance has increased much. the Question, that I have asked,the better answer that I have thought of is using iskindofClass. If image is coming from any class, on a condition ,we can check from where that image was coming and populate our image accordingly.
if ([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[UIImage class]])
{
cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"];
}
else if ([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[NSString class]])
{
cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"];
}
else if([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[NSURL class]])
{
[cell.contactImageView setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]]placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
Now, I’m able to populate the tableview properly.Once again thanx to scram.

iphone app - load cell image from XML element

I need some help to figure this out ..
I am getting data from the XML, one of the elements is the Image file Name "Icon1.png", images are save on my resources locally, at the moment I can load the icon1.png with this code
// retrieve an image
NSString *imageicon = [[NSBundle mainBundle]
pathForResource:#"icon2" ofType:#"png"];
UIImage *cellicon = [[UIImage alloc] initWithContentsOfFile:imageicon];
//set the image on the table cell
cell.imageView.image = cellicon;
but i want to load the icon file name from the XML element
<dimage>icon2.png</dimage>
how can I load the image to my cell ? - Solved
part 2 - selected row go to bclass description view controller,
-(void)viewDidLoad {
self.title = bclass.title;
[self.textView setText:bclass.description];
[self.destag setText:bclass.descimg];
}
at the moment "discing" is just showing the image name (UItextView), but i want to display the image on a UIImageView.
Thanks for your help.
Hernando.
Read the image file name from XML file to a NSString, and then create an UIImage using the
+(UIImage*)imageNamed:(NSString*)name class method of an UIImage object:
Example
NSString *imageName = ... read the name from xml.
cell.imageView.image = [UIImage imageNamed:imageName];

Cannot programmatically show image in UIImageView (iPhone)

I'm developing a iPhone app, but I ran into a snag.
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
[self setImage:new_image];
if (new_image == nil) {
NSLog(#"can't load image happy.1.jpg");
}
bgImageView.image = image;
That's the code I'm using to show an image, but it doesn't appear! new_image isn't nil, which I understand it would be if it couldn't load the image. bgImageView is an IBOutlet and connected in IB.
Please tell me what I'm doing wrong. Thanks!
Edit: also, this does not work on the simulator nor device
Is image nil?
What's wrong with [UIImage imageNamed:#"happy.1.jpg"]?
So, usually this is because the images are not getting copied into the built executable. So, open the application package and see if they are there.
If then maybe check that fileLocation doesn't come back empty/nil.
I wonder if the ".1" is causing a problem. Try renaming you image file _1, _2 and see if that fixes it.
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
bgImageView.image = new_image;
You can use
imageview.image=[UIImage imageNamed:#"imagename"];
and by using same we can show large number of images on each imageview.It will not cause the memory issue.just need to take care of proper release for allocated imageview.

image downloading from URL in iPhone application

I am using google chart functionality in iPhone app....I want to download image and put it into UIImageView from http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World ...
Is there any sample app for doing such functionality?or any other way to display charts in app?
Thanks in advance...
This would block, use NSURLConnection for non-blocking download
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
myImageView.image = downloadedImage;
Try imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]]
#iSwap - I know that why your code is not working.
i think you put an url as a string instead of url.
as above code ----
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
there is aURL that should be a url not a string, and u wrote it as a string like
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
you have been forget to convert this string into an url.
the right method will be like this-
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]]];
i hope you got it, and one more thing there is no difference between "willcodejavaforfood" and "CodeBrickie" code. just way of writing is little differ.