0

I need to allow the user to add photo either from source type camera or photo library when the user taps on the UIImageview.

I don't want to present a uiimagepicker controller by clicking on a UIButton, I want to allow the user to add it by tapping on the imageview.

How can I achieve this? Should I add the button the imageview, and when a tap occurs, present an action sheet with buttons camera and photo library and when the user selects one of the 2 buttons from action sheet allowing him to add photo to the uiimageview?

jrturton
  • 118,105
  • 32
  • 252
  • 268
jessy
  • 491
  • 3
  • 7
  • 19

2 Answers2

2

You can still use a UIButton, just have that button's view as the image you want.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
UIImage *img = [UIImage imageNamed:@"myimage.png"];
[button setImage:img forState:UIControlStateNormal];

This will allow you to use an image as your button.

Amit Shah
  • 4,176
  • 2
  • 22
  • 26
  • Thanks for reply. I don't want to set the image for the UIbutton. Instead of button, can i set the image let's say addphoto.jpg to the imageview by default and when i tap on it, actionsheet would pop up with options like take photo or choose photo. then the imageview should be updated with the image taken by the user. when the imageview is updated with the new pic then a label which "Edit photo" should be appeared on the image, so that when I tap on the imageview again, action sheet should pop up. If nothing is selected, imageview should have a addemployee.jpg which is the default image. – jessy Jan 31 '12 at 20:47
  • I don't see why you wouldnt want to use a button, its a lot easier, and what it is built for. But saying that you can use Gesture Recognizer. Tis post shows an example using pinch, but it's the same process http://stackoverflow.com/questions/3907397/gesuturerecognizer-on-uiimageview – Amit Shah Feb 01 '12 at 10:23
  • You could also have your image view how you like, but put a button over the top which will respond to the taps. – Amit Shah Feb 01 '12 at 10:24
0

@Amit Shah has the right answer - you should use a button.

But if for some reason you MUST use a UIImageView (and it doesn't sound like you do) - you can subclass UIImageView and override the touchesBegan/touchesEnded methods:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

shein
  • 1,834
  • 15
  • 23
  • you r right,we can proceed in the way which you said too. But my requirement is like the way which I asked for. Anyway, I was able to come over it.I have added a default img to imagevew and added guesture recognizer to it.All I want is when I tap on it, the old one needs to be replaced with new one.I don't even have to add the touches began n touches ended methods to it. Anyway thanks once again for your help. – jessy Feb 01 '12 at 15:04