I'm trying to build a menu using a UItableView with Custom cells which have an image and some additional text. However, when I try to load the image using the code below I get a SIGABRT error, why?
imageTmp = [UIImage imageNamed: #"HR1.jpg"];
cell.image = [UIImageView setImage:imageTmp];
The UIImageView is called image
shouldn't it be
cell.imageView.image = imageTmp;
Related
This code:
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"recipes"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"time"];
shows the title and subtitle of the cell, and takes data from JSON file.
This code:
cell.imageView.image = [UIImage imageNamed:#"test.jpg"];
shows the same static image to all the cells in the table view. How can I make this line of code something like the first two ones, so it will grab the image link from the JSON file and display the image as thumbnail?
You need to download the image first.
This question has been asked about a thousand times here:
Set UIImageView image using a url
Easy Asynchronous Image Download UIImage View
Download Images, Display Them in UIImageView, and SORT
iPhone SDK: Download and Set UIImageView before first view controller is displayed
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[news objectAtIndex:indexPath.row] objectForKey:#"URLOfYourImage"]]]
If you use this category you can then do it with one line of code.
[cell.imageViewPhoto setImageWithURL:[NSURL URLWithString:#"www.so.com/image.png"] placeholderImage:[UIImage imageNamed:#"placeholder.gif"]];
Moreover this category offers you lazy loading and caching capabilities which I think is important for what you need. I use it a lot in my projects.
Before i click the image to pull the camera, i want to set up a default image on the imageview that shows a http://www.inc.com/images/avatar/default1.gif to inform that in that imageview goes a person image.
What is the best way to do this procedure?
Best regards.
If you are using a UIImageView the first thing is to create a UIImage object using this code
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
And then pass it to the UIImageView object if it is a property of your view controller you can use this(imageView is the variable name of UIImageView)
self.imageView.image = defaultImage;
If you are instantiating a new UIImageView in your view controller you can use this
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIImageView *theImageView;
//assuming that's the declaration in your header
//make sure default1.gif is inside your application bundle
//copied into the resources folder of your xcode project
//this code goes into your -viewDidLoad method
theImageView.image = [UIImage imageNamed:#"default1.gif"];
I have a tableView based app and I made a custom TableViewCell that have a thumbnail in it.
I wish to programmatically choose a default thumbnail image for my custom TableViewCell.
any ideas on how to do that?
Thanks.
Set tag for your UIImageView in your custom UITableViewCell, for example, let it be 10.
When you want to show image in that view for some UITableViewCell *cell; do next:
UIImageView *imageView = (UIImageView *)[cell viewForTag:10];
imageView.image = [UIImage ....];
- (void)setCellIconImage:(UIImage*)iconImage
{
self.imgView.image = iconImage;
}
I have a UIImageView that I have already set an image to. This works fine, but later, I want to change that image. So, I set a new image for the image property, but when I view the app, that image is set as a second image over the first:
UIImageView *image;
image = (UIImageView *)[cell viewWithTag:0];
image.image = [UIImage imageNamed:#"History_Row2.png"];
How can I replace the current image in my UIImageView rather than seeming to add another?!
I did it by retaining the UIImageView, and keeping a reference as an ivar, releasing on dealloc.
image = (UIImageView *)[cell viewWithTag:0];
That was returning a UITableViewCell rather than a UIImageView, this is because the tag 0 is the default and it needs to be changed to a different number. After doing this it worked.
Im trying to load a single image into the grouped table cell. I found a piece of code on the internet and modified it. Original code is using UIViews to display image, which is not what I need, however original code works of course. PLease help me to make this code work to display an image in a sigle cell for grouped tableview. My TableView has only one row and it has UITableViewCellStyleDefault style.
Original Code
Here is my modified method, this is the core method.
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
[connection release];
connection=nil;
UIImage *img = [UIImage imageWithData:data];
self.image = img;
self.frame = self.bounds;
[self setNeedsLayout];
[data release];
data=nil;
}
This is how I use it to display image in my TableView Cell cellForRowAtIndexPath method.
CGRect frame;
frame.size.width=75; frame.size.height=75;
frame.origin.x=0; frame.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
[asyncImage loadImageFromURL:imageURL];
cell.imageView.image = asyncImage.image;
In that code you posted, the image of the AsyncImageView doesn't get set until after the connection finishes loading and it actually creates the image. Problem is, you're setting it's image into the cell immediately. That's not going to work!
You're going to want to make a custom cell class (subclass UITableViewCell) and use that cell in your table. There's lots of sample code showing how to use custom cells and lay them out with views. Instead of using UITableViewCell's standard imageView, create your own layout and use an AsyncImageView to show your image. Then it should work.