How to detect objects from Image in iOS? [duplicate] - iphone

This question already has an answer here:
How to detect objects from Image Processing in iOS?
(1 answer)
Closed 9 years ago.
I have an image, on an image there are three things Spoon, Cup and basket.
Now I have to identify when user click on image nothing will happen until and unless the user click on spoon, cup & basket.
If user click on spoon the spoon colour will become green.
Same for cup and basket.
And I have no idea how to do this. I want some hints from experts and how and where to start?
Any idea or suggestions would be highly welcome.

You can try out masking of views by setting the visible parts as the images of objects and set that view with the gestures
or
Make different images for the objects set it in the selected state of button and on button click change the state to selected
[button setSelected:YES];
[button setSelected:NO];

Place buttons on Spoon, Cup and basket. set button.backgroundcolor = [UIColor clearcolor]; Change images in IBAction.

lets say your image is in backgroundImageView. You got to keep 4 images. The original image, image with green spoon, image with green cup and image with green basket. And then add custom style buttons on top of spoon, cup and basket. And connect below IBAction methods to your buttons.
- (IBAction) spoonButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_SPOON.png"];
}
- (IBAction) basketButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_BASKET.png"];
}
- (IBAction) cupButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_CUP.png"];
}

You can start investigating UIButton.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/
Make your images as Button background image in Interface builder or in code. You can do pretty much anything with image then.

Adding Buttons on your object. and go file inspector click on 5th tab make that button clear color your button dissapear.
then do what you want.
also tried to set image directly on buttons
[ButtonSpoon setImage:[UIImage imageNamed:#"spoon.png"] forState:UIControlStateNormal] ;
[ButtonCup setImage:[UIImage imageNamed:#"cup.png"] forState:UIControlStateNormal] ;
[ButtonBasket setImage:[UIImage imageNamed:#"basket.png"] forState:UIControlStateNormal] ;

Related

IOS Custom UIButton

I currently work on a small application in which I have several UIButtons. Each of them has an UIImageView beneath it. At the moment, I associate each UIButton with each UIImageView (all are named via an IBOutlet - UIButton and UIImageView). When I press a button, the underlying image changes. I would like to know if it's possible (and how) to create a button which contains an underlying UIImage (not a UIButton image or background image), a sort of new object.
This image is slightly larger than the associated UIButton, like the apple calculator basic apps, and this image changes when I press the button.
I've tried several solutions (certainly incomplete) and I'm beginning to despair.
My current solution is functional but I do find it not particularly elegant.
UIButton has both image and backgroundImage as its properties.
To achieve what you are looking for, I would actually recommend you to stick to just a UIButton, and then using contentInset to position the image over the backgroundImage so it creates the effect I think you are looking for. This should be quite easy in interface builder...
you can make an invisible button and behind it make the image..
This will give you what you want but should be avoided at cost...
Stick to UIButton..
I believe even in calculator app they are only UIButtons and nothing else..
they have been coded to been momentary selected ..so that's why you see them kind of changing highlight for a small fraction and each button have been coded to perform specific function(+ - / equal .)
Update : i might be in doubt but if you asked for a button to have an image view inside it..
then here is the answer.
Make UIbutton. Make an ImageView and then call [theButton.view addSubview:ImageView];
Since both UIbutton and UIImage view inherit from UIView.. you can add as many subviews to them..that is inside them
If I understood your question correct then you may try the following:
UIButton *button = [UIButton alloc]init];
[button setImage:buttonImage forState:UIControlStateNormal]; when button not pressed
[button setImage:buttonImage forState:UIControlStateSelected]; when button tapped
[button release];

how to show active button in iphone app

I am developing an application in which there are 7 buttons in every view.
We can switch on any view with respective to button clicked.
Now I want to enable(by change color or background or any thing) the clicked button, means all buttons should remain as it is only the clicked button should look different.
you can use
[button setImage: forState:];
This will help if u are using images for the button
[button setBackgroundImage: forState:];
You have two images like "selectedImg.png" and "unselectedImg.png" and change them according to the selection.To change the image use the following setter method,
[selectedButton setBackgroundImage:selectedImg.png forState:(UIControlState)state]
In the case of "setImage:" the image size will not resized for your button size.But in "setBackgroundImage:" will resize and set that as background.

Half of UIButton not responding to touch events

I have a UIView - which I am using as a makeshift toolbar. It contains several UIButtons. (Six, I think) in a horizontal row.
The fifth button over will only respond to [TouchUpInside] events when I click the left half of it - whereas all the other buttons work correctly.
I have gone crazy making sure there weren't any other views overlapping it, etc - and everything seems okay.
Any idea of how to investigate further? Is there any way to see an "event inspector" to see where the touch messages may be going?
Most likely, the other half is covered by a transparent view that obstructs it. See the frames of all sibling views of the button:
for (UIView *v in myButton.superview.subviews){
NSLog(NSStringFromCGRect(v.frame);
}
And see if any of the frames above the button (the array is ordered from bottom to top) overlap with it.
If they don't, see if the whole window has any views covering the button.
The problem was as follows:
The "sixth" button was a "Info Light" type button. For some odd reason - even though the bounds of this button were clearly outside of the bounds of the offending "fifth" button - the "info light" button seems to acquire touches a bit outside it's bounding box.
When I either:
Moved the "Info Light" button further away from the "fifth" button
-or-
Changed the "Info Light" button to a regular (Round Rect) button
...the problem went away!
See the two rightmost buttons in this image:
I guess your superview bounds might be a bit too small? Try to change your superview background color to red or some vivid color and check its bounds.
Well you could try to use this technique with NSLog() to log events and get a sense of what might be happening. I'm not sure if there's a better way to log events:
Observing pinch multi-touch gestures in a UITableView
Just ran into this strange problem as well and after struggling with understanding the nature of the problem, ran across this post. Thanks for the helpful tip! I know it's an old question but this is how I was able to work around it in two ways. The first was to initially create the system info light button and simply retrieve the button image and create a custom button with that image, ignoring the initial button. The advantage to this is that you always get the latest info light graphic in case it changes in the OS. The disadvantage being that a new version of the OS might decide at some point to not make the image available in this way.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImage *img = [button imageForState:UIControlStateNormal];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:img forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:#selector(infoSelected:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[barItems addObject:barButton]; // NSMutableArray of tab bar items
The second approach, which I decided to use, so that I knew it would be available, and also to match my splash screen art in case the OS graphic changed, was to follow the same technique and simply save the original PNG to disk and add it to the project as a custom image for the button.
// Run these four lines just once in the simulator to save the original info
// light button image to disk and then eliminate these four lines and create the
// custom button with the image added to the project. Note that it may be a 1x or
// 2x image depending on the simulator device. So run it on both to get two
// versions (changing the name of course).
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImage *img = [button imageForState:UIControlStateNormal];
NSData *imgData = UIImagePNGRepresentation(img);
BOOL saved = [imgData writeToFile:#"/Users/Shared/InfoLight.png" atomically:NO];
I noticed that upon selecting the infoLight type, the button could no longer be interacted with. By checking the checkbox "User Interaction Enabled" I solved the problem.

how to set image in button

How set image in button when ever click the button image show me that button
run.backgroundColor = [UIColor redColor] I try this but it doesn't work,
To set an image for a button while it is currently being touched (I think this is what you're asking). You use setImage: forState on the UIButton.
[myButton setImage:[UIImage imageNamed:#"myButtonGraphic.png"] forState:UIControlStateHighlighted];
You can control when the image appears using the different control states, one of:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
UIControlStateApplication
UIControlStateReserved
If you use UIControlStateNormal, the image is displayed all the time, and when the button is currently being touched it will appear dimmer which for a lot of cases is good enough to indicate to the user that they are selecting that button and that it's a real touchable 'thing'.

Question regarding UIButton

everyone
I have 1 UIButton with 1 image with blue colour
but when i click on UIButton at that time i want to remove that image and add 1 another image.
In code you can set button image following way:
[yourButton setImage:yourImage forState:UIControlStateHighlighted]; // or for whatever state you want
You can also set all necessary images in IB for all possible states (default, highlighted and selected)