iPhone dev question help forming array - iphone

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)

Related

UIImage isKindOfClass issue

This is my code in my application,
[imageview setAlpha:1.0f];
[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[pages objectAtIndex:swipeCount]]]];
[imageview setFrame:CGRectMake(-300, 0, 1368, 1000)];
Edit:
pages = [[NSArray alloc] initWithObjects:#"page1",#"page2",#"page3",#"page4",#"page5",#"page6",#"page7b",#"page8",#"page9",#"page10a",#"page11",#"page12",#"page13b",#"page14",#"page15",#"page16a",#"page17",#"page18",#"page19",#"page20",#"page21",#"page22",#"page23",#"page24",#"page25", nil];
imageview=[[UIImageView alloc]init];
its working properly, problems except when the app enters background and comes back to foreground shows the following error,
*** -[UIImage isKindOfClass:]: message sent to deallocated instance 0x1e8b10
What wrong with the code?
Please help me out
Yes, you need to allocate memory to your UIImage. What is basically happening is that your image is being temporarily stored in the memory and deallocated upon closing the app so iOS could allocate that memory to more immediate needs. You can fix that as below. I'm also gonna allocate a string since stringWithFormat returns an autorelease String.
NSString *imageName = [[NSString alloc] initWithFormat::#"%#.png",[pages objectAtIndex:swipeCount]];
UIImage *myImage = [UIImage imageNamed:myImage];
[imageview setImage:image];
Try using properties. Make array,imageView as a property and then use
self.pages and self.imageView
see this good article on properties
Objective-c properties

_NSCFConstantString CGImage]: unrecognized selector sent to instance

App is crashing with error _NSCFConstantString CGImage]: unrecognized selector sent to instance
when trying app with this code to save image to photo album
int currentimage = _imageScrollView.contentOffset.y / [pictures count];
UIImage *imageToShow = [pictures objectAtIndex:currentimage];
UIImageWriteToSavedPhotosAlbum(imageToShow, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
By the information that you gave in the comments, you are using an UIImage reference ("imageToShow") that is pointing to a NSString (element from the array), and that is why when imageToShow receive the CGImage selector, crashes.
To solve this problem, you have to put UIImage references to UIImage objects in the array.
UIImage *image0 = [UIImage imageWithContentsOfFile:"image0FullPath.png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:"image1FullPath.png"];
pictures = [[NSArray alloc] initWithObjects:image0, image1, nil];
If the images are in the mainbundle, you can use imageNamed instead of imageWithContentsOfFile. imageNamed receive as input only the name of the file, and search for it inside the mainbundle. imageWithContentsOfFile needs the full path.
Good Luck!

UITableView image causes crash

I've done a million UITables - with subtitles, images, backgrounds, colors, text-styles - you name it.
Suddenly, I'm crashing on this table, specifically on the line that calls for the image of the cell.
Here's the code:
// Configure the cell:
cell.textLabel.font = [UIFont fontWithName:#"Franklin Gothic Book" size:18];
cell.textLabel.text = [leadershipMenu objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [leadershipSubtitlesMenu objectAtIndex:indexPath.row];
// And here's the statement that causes the crash:
cell.imageView.image = [leadershipPhotosMenu objectAtIndex:indexPath.row];
Now, the error I get is this:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString _isResizable]: unrecognized selector sent to instance 0xcacbc'
I know for sure that the statement causing the crash is the
cell.imageView.image = ...
cause as soon as I comment it out everything works fine.
I've never in my life seen an
-[__NSCFConstantString _isResizable]:
error.
I've googled it but found very little on it.
Very peculiar.
Anyone out there got any clues?
as mentioned in your comment. the way you save your image is what causes the problem.
try this..
leadershipPhotosMenu = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"JohnQ.jpg"], [UIImage imageNamed:#"BillZ.png"], nil];
the code above will store the images in your mutableArray, That will work but I suggest not to store the images in an array.
you can also solve your problem without storing your images in your array like the code above by doing:
cell.imageView.image = [UIImage imageNamed:(NSString*)[leadershipPhotosMenu objectAtIndex:indexPath.row]];
this error message means your object inside your leadershipPhotosMenu is not image, but string
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString _isResizable]: unrecognized selector sent to instance 0xcacbc'
Do this:
cell.imageView.image = [UIImage imageNamed:[leadershipPhotosMenu objectAtIndex:indexPath.row]];
You are storing the name of the images ad not the images. However imageView has UIImage as its property and not the image name. So make below change.
cell.imageView.image = [UIImage imageNamed:[leadershipPhotosMenu objectAtIndex:indexPath.row]];

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.

warning regarding the cell of UITableviewcell

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"]];