HI all I am developing an iOS app which allows the user to pick an image using UIImagePicker. Now I have to create the action that restores the UIIageView with no image inside. can anyone help me with code?
-(IBAction)emptyImage{
//code to empty the UIImageView
}
-(IBAction)emptyImage{
imageView.image=[UIImage imageNamed:#"noimage.png"]; (OR)
imgview.image= nil; (OR)
imgview.image= [UIImage imageNamed:#""];
}
Try to use this one....
-(IBAction)emptyImage
{
imageView.image=nil
}
-(IBAction)emptyImage
{
NSArray *remoVeSunBVIew = [self.view subviews];
for(UIView *viw in remoVeSunBVIew)
{
[viw removeFromSuperview];
}
}
Hope this will help you..
Related
I am trying to comparing image with button images but my code is not working for this requirement. What did I do here wrong? Please help me.
NSString *btnImage;
UIButton * sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([sharebutton.imageView.image isEqual:[UIImage imageNamed:#"arrow.png"]])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}
you can check image for specific state
if([[sharebutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"arrow.png"]])
{
//Image found
}
Maybe this will help you.
if (sharebutton.currentImage == UIImage(named: "arrow.png")) {
}
U will need to convert this into obj-c but i think u will get it. :)
You could use the following code:
if ([UIImagePNGRepresentation(firstImage) isEqualToData:UIImagePNGRepresentation(secondImage)])
If you want to compare button image with another image name then set your button image this way:
sharebutton.setImage(UIImage(named: "arrow.png"), forState: UIControlState.Normal)
Remember one thing here don't set background image for button.
Now you can compare button image with another image this way:
if (sharebutton.currentImage?.isEqual(UIImage(named: "arrow.png")) != nil) {
println("YES")
} else {
println("NO")
}
UIImage *image = [sharebutton imageForState:UIControlStateNormal];
if ([image isEqual:[UIImage imageNamed:#"arrow.png"]]) {
}
Do this,you can solve it.
This is what i'm using:
NSData *leftImageData = UIImagePNGRepresentation([sharebutton imageForState:UIControlStateNormal]);
//take note that using `imageForState:` only works if you set your image using `setImage:forState:`
//if you are using `setBackgroundImage:forState:`, `[shareButton currentBackgroundImage]` is what you need.
NSData *rightImageData = UIImagePNGRepresentation([UIImage imageNamed:#"arrow.png"]);
if ([leftImageData isEqualToData:rightImageData])
{
//same
}
else
{
//not same
}
For performance reasons I'd compare the file names rather than creating a temporary image which is much more expensive
NSString *btnImage;
UIButton *sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([btnImage isEqualToString:#"arrow.png"])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}
Of course this requires to maintain the variable btnImage to hold always the current file name
Try this:
UIButton *sharebutton;
UIImage *image1 = [UIImage imageNamed:#“arrow.png"];
[sharebutton setImage:image1 forState:UIControlStateNormal];
if (sharebutton.currentImage == [UIImage imageNamed:#"arrow.png"])
{
//do something here
}
I am creating an app in which I have used user registration.In registration page, I have created many UITextFields.I wants that when a user select any TextField then its background Image is change. I uses the code to do that is:
- (IBAction)touchBegin:(id)sender
{
//UILabel *opt = (UILabel *)[cell viewWithTag:101];
if (_text1.isSelected) {
_text1.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text2.isSelected){
_text2.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text3.isSelected){
_text3.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text4.isSelected){
_text4.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else{
}
}
But I am not getting the result according to my need.
What should I use to get correct output?
be sure whether your textfield border style is UITextBorderStyleNone.
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"image.png"];
I think that it's better and easier to use UITextFieldDelegate methods like this, for example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"big_star_non_rating"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"AnImageForNormalStateOrSetNilBackground"];
return YES;
}
Hope it helps.
I'm trying to set an image from another ViewController using properties. But It doesn't work. Someone said that you only can change image from inside the Class. But i tried and its not working either way. Any ideas?
//viewcontroller1
ViewController *viewcont = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:viewcont animated:NO completion:nil];
[viewcont somemethodThatIscallemFromOutside];
//viewcontroller2
-(void)somemethodThatIscallemFromOutside{
if (!r12 && g12 && !b12) {
green.image = [UIImage imageNamed:#"Green"];
self.Greenscore.textColor = [UIColor greenColor];
self.readyplayer2.image = [UIImage imageNamed:#"Greenready"];
}
}
First of all no such method available
[UIImage imagename:#"Image1.png"]
Replace with
[UIImage imageNamed:#"Image1.png"];
you should see also myimageview is intiliazed or not?
Try this:
myimageview.image = [UIImage imageNamed:#"Image1.png"];
Hope this helps.. :)
Replace this with your code,your mistake in "_" which is be"="
[myimageview setImage:[UIImage imageNamed:#"Image1.png"]];
[_firstPrincipleButton setBackgroundImage:[UIImage imageNamed:#"i_press.png"]
forState:UIControlStateNormal];
The above code is working fine in iOS7 but in iOS7.1 this is not changing the image of UIButton. I have also tried to do this with setImage but didn't work.
Thanks
Just in case that others have the same problem and need another hint on how to fix this:
I also had this problem and finally found out that setting a button's (background) image doesn't work on iOS 7.1, if the button is disabled.
Not sure if this fixes your problem, but it was mine. Calling setNeedsLayout didn't help in my case, unfortunately. What you can do as a workaround is either to override UIButton or to add a category that contains a method like the following to set an image:
- (void)setImage:(UIImage *)img forButtonState:(UIControlState)state
{
BOOL shouldEnable = self.enabled;
self.enabled = YES;
[self setImage:img forState:state];
self.enabled = shouldEnable;
}
Filed a bug report for this issue (16497031).
It is a bug in iOS 7.1 ,
you can create a category for UIButton and implement a method to set the image somewhat like this.
-(void)setImage:(UIImage *)image forButtonState:(UIControlState)state
{
CGFloat version = [[UIDevice currentDevice] systemVersion].floatValue ;
if( version <= 7.1)
{
BOOL isFirstTime = YES ;
for(UIView *aView in self.subviews)
{
if([aView isKindOfClass:[UIImageView class]])
{
[(UIImageView *)aView setImage:image] ;
isFirstTime = NO ;
break ;
}
}
if(isFirstTime)
{
CGRect frame = self.frame ;
frame.size = image.size ;
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setImage:image];
[self addSubview:imageView];
}
}
else
[self setImage:image forState:state];
}
This will work.
enjoy coding :)
I found a property called imageForState in xcode but am having trouble getting it to do what I want. When a button is pressed, I want to execute a block of code depending on the button's image.
- (IBAction)favButton:(UIButton *)sender {
NSString *currentImage = [sender imageForState:UIControlStateNormal];
if([currentImage isEqualToString:#"already_fav"])
{
// execute code
}
}
However, I am getting the error:
Incompatible pointer types initializing NSString _strong with an expression type of UIImage
Can someone please tell how to get around this?
I am sure that you are not able to compare a string with an image. There is a solution however. All you have to do is set the tag for the image you are wanting to compare, and set the tag of the image you are comparing to.
image1.tag = 1;
image.tag =1;
if(image1.tag == image.tag) {
// execute code
}
that should help, and I hope that it does.
SO, for this exercise, i'll show you. Change the NSString to a UIImage
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
currentImage.tag = 1;
wantedImage.tag = 1;
if(currentImage.tag == wantedImage.tag) {
// do something
}
hopefully this helps you out :)
When you create a UIImage it is just an image and has no string value attached. The name is not carried by the UIImage object.
If it were me (and this isn't the only solution, just another suggestion), i'd create individual UIImage objects (depending on how many we're talking about here) as this should checkable while strings are not.
For example:
UIImage *image1 = [UIImage imageNamed:#"randomName"];
UIImage *image2 = [UIImage imageNamed:#"anotherRandomName"];
[myButton setImage:image2 forState:UIControlStateNormal];
if ([myButton imageForState:UIControlStateNormal] == image1) {
NSLog(#"The button shows image 1 for normal state");
}
else if ([myButton imageForState:UIControlStateNormal] == image2) {
NSLog(#"The button shows image 2 for normal state");
}
else {
NSLog(#"Error!!!!!!! :D");
}
you cant save the Image as a String
try
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
than you can check:
if(currentImage == [UIImage imageNamed:#"already_fav"])
I did this for switching sound/noSound image on button:
- (IBAction)soundAction:(id)sender {
UIImage *sound = [UIImage imageNamed:#"sound"];
UIImage *noSound = [UIImage imageNamed:#"noSound"];
if ([[sender currentImage] isEqual:sound]) {
NSLog(#"IF SOUND IMAGE");
[sender setImage:noSound forState:UIControlStateNormal];
//your code
} else if([[sender currentImage] isEqual:noSound]) {
NSLog(#"IF NO SOUND IMAGE");
[sender setImage:sound forState:UIControlStateNormal];
//your code
}
}