warning regarding the cell of UITableviewcell - iphone

cell.image = [UIImage imageNamed:#"search_up.png"];
whenever i'm writing this statement i get warning like
deprecated
if i use
cell.textLabel.image = [UIImage imageNamed:#"search_up.png"];
i got error
how to remove this warning or error

[cell.imageView setImage:[UIImage imageNamed:"search_up.png"]];

Related

iOS TableView Cells

I have found many answers to problems I've encountered on here and would like to ask a question for the first time (I'm a newbie to asking for help!)
I've implemented the SDWebImage framework into an project that uses JSON parsed data to populate a UITableViewController. The images are being cached successfully but when the App first launches, all the cell images are absent. Only when I scroll up or down and the rows appear (or when I tap them to select a row) do they appear. If I go back into it, all the images are there cached and work fine. It's just the first launch that I am missing all my images. I've pasted what I have here in the hope someone can post a simple solution. Got a feeling it's something minor that I have to do to make them appear on first launch.
Code follows:
static NSString *MyIdentifier = #"Maincell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
}
UIFont *wireBold = [UIFont fontWithName:#"Helvetica" size:14.0f];
cell.textLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:#"Title"];
cell.textLabel.font = wireBold;
cell.textLabel.textColor = [UIColor blackColor];
UIFont *wireSub = [UIFont fontWithName:#"Sintony" size:10.0f];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row]objectForKey:#"Name"];
cell.detailTextLabel.font = wireSub;
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *filePath = [[news objectAtIndex:indexPath.row]objectForKey:#"Image"];
NSString *urlString = [NSString stringWithFormat:#"http://www.mywebhost/images/%#.png",filePath];
[cell.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
return cell;
Are you at least seeing placeholder.png initially? Is that image appropriately sized for your cell? Reading through comments in other posts, it appears that if you do not have a placeHolder image then it won't show the loaded image until the tableView is reloaded. That is consistent with the behavior you reported.
I also recommend you checkout JSON fetching frameworks such as Sensible TableView. The framework will automatically fetch all your JSON data from your web service, and will handle all display details including fetching the images asynchronously. Saves me tons of time.

iOS: UIImage EXC_BAD_ACCESS

I am building a small iPhone only application (iOS SDK 6.0) and I am getting EXC_BAD_ACCESS exception in XCode.
The application has UITableView with custom UITableViewCell. I have implemented the UITableViewCell in a subclass (I call it MyCustomCell here) and added another parameter to the constructor:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellImage:(UIImage *)image;
The UIImage is added to contentView of the TableCell in the method, with following code:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
imageView.image = image;
[self.contentView addSubview:imageView];
This function is called within the function cellForRowAtIndexPath of course:
static NSString *CellIdentifier = #"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier cellImage:self.cellImage] autorelease];
}
return cell;
The cellImage property of the ViewController (also the UITableViewDelegate) is declared as (it is also synthesized):
#property (nonatomic, retain) UIImage* cellImage;
The image is initialized in viewWillAppear method of ViewController with the following line:
cellImage = [UIImage imageNamed:#"my_image_resource.png"];
This code works correctly, but the EXC_BAD_ACCESS appears, when I customize the image with iOS 6.0 only function:
cellImage = [[UIImage imageNamed:#"my_image_resource.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch];
Debugging helped me to locate the line that triggers the error - it is in the initWithStyle method of MyCustomCell implementation:
imageView.image = image;
I have looked through many EXC_BAD_ACCESS errors, I also read the tutorial of why does this error appear, but up to this point, I am not deallocating anything. Some object is probably released at this point, but I cannot find which one exactly and how could I fix the problem.
I have also tried creating two properties in ViewController class, one to hold the original image and the other image with insets. It does not solve the problem, however.
Why is this an error? Why it is not working with image with insets? How could I fix this problem?
Thank you.
The error appears when you execute the statement imageView.image = image;. Thus, the object image does not exist when this statement is executed. It must have been released before, either explicitly by a [release image] (since you don't use ARC), or implicitly if image is autoreleased (e.g. if you created it with [UIImage imageNamed:...]).
I think the best were to convert your code ARC, as Aaron Wojnowski suggested. Otherwise, check if image can be released unexpectedly.
You've created a property for cellImage, but you're setting it using the instance variable instead of the setter which has been created for you. Since you're passing in an autoreleased object, there is a possibility that it's been released when you try to reference it next time. Try using this below:
[self setCellImage:[[UIImage imageNamed:#"my_image_resource.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch]];
Or with this:
self.cellImage = [[UIImage imageNamed:#"my_image_resource.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch];
With this code, the image will be retained for you inside of the setter, and next time you try to use it, the memory will still be allocated and you shouldn't have any issues.
Consider also using ARC, if possible, as much of this intricate memory management stuff is handled for you. http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

UIImage imageNamed

I'm trying to use a url as a UIImage in the OpenFlow API.
NSString *imageUrl = [[[newsEntries objectAtIndex:0] objectForKey: #"still"] retain];
NSURL *url2 = [NSURL URLWithString:imageUrl];
NSData *photoData = [NSData dataWithContentsOfURL:url2];
UIImage *imageUI = [UIImage imageWithData:photoData]
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:myImage]];
[imageUr release];
[(AFOpenFlowView *)self.view setNumberOfImages:3];
I have tried it like this, but no success. The only way I got this API working was using the imageNamed type. The initwithData has no success.
So how can I change this NSString to finally become a imageNamed method?
A UIImageView is different from a UIImage.
Change this line: [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:myImage]];
To this: [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageUI]];
and it should work.
You have several significant errors here. It appears that you may need to read about C/Objective-C and types.
It sounds like you are asserting that, specifically, the line
UIImage *imageUI = [UIImage imageWithData:photoData]
is not working. The code up to that point actually looks okay (though it is not necessary to retain and then release the imageUrl variable.
Once you have created your UIImage, you should be able to pass it directly to your AFOpenFlowView:
[(AFOpenFlowView*)self.view setImage:imageUI forIndex:0];
The line
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
has two errors (and is unnecessary anyway). First, -initWithFrame takes a CGRect as its argument, not a UIImage. UIImageView does have an initialization method -initWithImage, which is probably what you intended. But either way, methods that start with "init" are instance methods, not class methods, so you have to call them on actual instances of a UIImageView, like this:
UIImageView *myImage = [[UIImageView alloc] initWithImage:imageUI];
Note that this will leak memory unless you autorelease or release it.
The following line, you correctly try to give a UIImage to your AFOpenFlowView, but you attempt to create that UIImage by passing a UIImageView to the +imageNamed method. +imageNamed takes an NSString that contains the name of the image, and passing anything else to it won't work.
You must always be aware of what kind of object a method is expecting to receive, and make sure you find some way to give it that kind of thing.
Probably what you are looking for here is something like this:
NSString *imageUrl = [[newsEntries objectAtIndex:0] objectForKey: #"still"];
NSString *imageName = [imageUrl lastPathComponent];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName]];

Table image not showing, "Pop an autorelease pool" error

I have a UITableView which uses the following code to display an image in a Table View cell:
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
UIImage *image = [UIImage imageNamed:[[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
if ( image ) {
cell.imageView.image = [image imageScaledToSize:CGSizeMake(50, 50)];
}
It works fine on the iPhone simulator, but when I try it on a real iPhone the iPhone doesn't show. Instead in the console in debugging mode, I get this error:
attempt to pop an unknown autorelease
pool (0x851e00)
Any help would be great, thanks.
Check that image != nil. If it == nil then problem in that [[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] code.
Generated file name must be equal real file name including extension, case and must not contain whitespaces.

iPhone dev question help forming array

I've spent this morning searching google for this, but I just can't get what I want how i want it.
I am creating a custom table view cell, with different icons down the side, in a sectioned table view. My problem is that I am having trouble reading these images from an array.
I can do it like below, but can someone please help me do this from an array.
Working Long Form Code:
switch (indexPath.row) {
case 0:
imageView2.image = [UIImage imageNamed:#"ico-company.png"]; break;
case 1:
imageView2.image = [UIImage imageNamed:#"ico-value.png"];
case 2:
imageView2.image = [UIImage imageNamed:#"ico-date.png"]; break;
case 3:
imageView2.image = [UIImage imageNamed:#"ico-notes.png"];break;
default:
break;
}
And i think i can get it to look something like:
Not Working Code how i want
arryImages = [[NSMutableArray alloc] init];
arryImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"ico-company.png"],
[UIImage imageNamed: #"ico-value.png"],
[UIImage imageNamed: #"ico-date.png"],
[UIImage imageNamed: #"ico-notes.png"], nil];
imageView2.image = [UIImage imageWithContentsOfFile:[arryImages objectAtIndex:[indexPath row]]];
And this is the error i get when i try my array code:
2010-01-18 13:20:47.314 SQL[60921:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage length]: unrecognized selector sent to instance 0x39135c0'
Regards
Your last line should actually be:
imageView2.image = [arryImages objectAtIndex:[indexPath row]];
(in your code, you're passing an image object to imageWithContentsOfFile, but that method expects a filename, not an image object)