In IOS (iPhone) - I want to show the user an Image, he should write a text string (like the image name or something), If he writes the right string the image changes to a different image, if he makes a mistake and type the wrong string, nothing happens
take user's entered string into
NSString *imageName = userTxtField.text;
UIImage *image = [UIImage imageNamed:imageName];
if(image != nil){
// change previous image
}
May be you are trying to build an app like "Brands"
Once user types and press submit button compare imagename with user string
if([textField.text isEqualToString:IMGNAME]) {
imageview.image = nextimage;
}
You should use ImageNamed function of UIImage class.
If return value is nil then an image doesn't exist.
For example:
-(UIImage*)getImageNamed:(NSString*)name{
UIImage* Image = [UIImage imageNamed:name];
if (!Image)
NSLog(#"Sorry, there is no image named %#.",name");
return (Image);
}
Related
NSString *devicetype;
if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)){
devicetype = #"ipad";
}else{
devicetype = #"iphone";
}
I have two images
BACKGROUND_IPAD.PNG
AND
BACKGROUND_IPHONE.PNG
Now I want to use this string like
BackGround = [CCSprite spriteWithFile:[NSString StringWithFormat:#"background_%#.png",DEVICETYPE]];
Is this possible?
If not, then is there a better way?
Yes you an use it, but insread of that you can change the name of the image to the following
Image~ipad.png
Image~iphone.png
Now when you load the image
//on the iPhone and iPad use the same image name
//image named will select the image automatically
UIImage *image = [UIImage imageNamed:"Image"];
It will automatically select the image basing on your current device
Yes it works. I have tested and running successfully.
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];
Hi I have this code to download images from the server and then to show them in table view with some text (if there is no image I want to show the text anyway):
//download part
NSMutableString *photourl=[[NSMutableString alloc] initWithString:url_image];
[photourl appendString:[dic objectForKey:#"photo"]];
#try{
//add to mutable array
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photourl]]];
}
#catch (id Exception) {
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_image]]];
}
//part of table view code
NSData *imageData = [photoSmallImageData objectAtIndex:indexPath.row];
#try {
cell.imageView.image = [UIImage imageWithData:imageData];
}
#catch (NSException *exception) {
cell.imageView.image = [UIImage imageNamed:#"icon72.png"];
}
It works but the thing is sometimes there is no image so I would like to replace it with some icon but since we can't add nil to array i juste add some part of my link there and then even if the link doesn't work for image data it doesn't call the catch method. I don't know if I could some how replace the url with local url but I don't know how. Also I can't skip the step because then the images will not correspond to the text next to it.
2.The images are all of diferent sizes and I would like them to have standard look and be centered is there a way to do that and at which point of the code? Thank you very much
EDIT I have found the method:
dataWithContentsOfURL:options:error:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
but I don't know how to use it
Why don't you create the images when the image data is downloaded and keep them in the array? Also, you are right to be after the dataWithContentsOfURL:options:error: method. Given that you keep the image names in the dic dictionary, the code would look like this:
NSMutableString *photourl=[[NSMutableString alloc] initWithString:url_image];
[photourl appendString:[dic objectForKey:#"photo"]];
//try to create the image
NSError* err = nil;
UIImage* img = nil;
NSData* tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:photourl] options:NSDataReadingMappedIfSafe error:&err];
if (!err) {
// image data loaded, create image
img = [UIImage imageWithData:tempData];
} else {
// image data could not be loaded, display error
NSLog(#"%#", err);
// put the default image in the array
img = [UIImage imageNamed:#"icon72.png"];
}
[photoSmallImage addObject:img];
And then in the table view delegate:
cell.imageView.image = [photoSmallImage objectAtIndex:indexPath.row];
As for the second part of your question, you may want to have a look at the contentMode property of UIView class. The default is scale to fill. By manipulating this property on the cell.imageView, you can achieve the effect that you desire.
How may I modify, read in an image path from SQLite DB, & display it via an UIImageView? In other words, this application will work in offline mode & i have the images at hand. How will i go about this then, tgt with sqlite? (retrieve images by storing their image path name # sqlite)
Sample code I have for now:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
//start with index 0
currentStepIndex = 0;
Resolution *resolutionObj = [appDelegate.resolutionArray objectAtIndex:currentStepIndex];
[resolutionDescription setText:resolutionObj.stepDescription];
//checking for null string
//replicate of ifelse #goToNextStep & #goToLastStep (needed for initial load of image)
if (resolutionObj.imageDescription != NULL)
{
//if goes in here, then image description is not null
NSURL *imageURL = [NSURL URLWithString:resolutionObj.imageDescription];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[resolutionImage setImage:[UIImage imageWithData:imageData]];
}
else
{
//if goes in here, then image description is NULL
//show empty image
[resolutionImage setImage:NULL];
}
}
You are missing a lot the way I see it. If your images are coming from a website then you should use a webView to display it. If you are planning to display images in UIImageView, you should first download them onto your device from the url that points to that image and save it in the sandbox. Then you store the path of this downloaded image in your sqlitedb. Now when you want to load this image you just access this image using the path that you have in your sqlitedb and render it. How do you think by just saving the imageName image013.png and not downloading the image itself will render the image for you in UIImageView. Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. For going and finding on the web directly, you need a UIWebView and not an UIImageView
If you are packaging your images as resources with your app binary, you can load them as shown below:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myimagefile" ofType:#"png"]];
[self.testView.imgView setImage:img];
where myimagefile is the name of your file and ofType is the type of image. .png or .jpg or whatever.
If you have anymore doubts. Please come back.
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.