I have a question on UIImagePickerController. May I use UIImagePickerController as a preview? If I do like that, I have to hide record button, is there any way to do it? or Any other approach to show our preview other than UIImagePickerController.
thanks for your helps...
You can hide the controls of a UIImagePickerController by using:
[myImagePickerController setShowsCameraControls:NO];
EDIT: Check out this code, it hides all the controls inside the image picker allowing you to create and add your own custom controls. Unfortunately you can not selectively hide controls within the picker so this is the alternative.
- (void)showCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
myImagePicker = [[UIImagePickerController alloc] init];
[myImagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[myImagePicker setDelegate:self];
[myImagePicker setShowsCameraControls:NO];
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIButton *switchCameraButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[switchCameraButton setFrame:CGRectMake(10, 430, 72, 40)];
[switchCameraButton addTarget:self action:#selector(changeCamera:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:switchCameraButton];
[myImagePicker setCameraOverlayView:overlayView];
[self presentViewController:myImagePicker animated:YES completion:nil];
}
}
- (IBAction)changeCamera:(id)sender
{
if (myImagePicker.cameraDevice == UIImagePickerControllerCameraDeviceFront) {
[myImagePicker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
}else{
[myImagePicker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
}
}
Related
I'm playing around with a cameraOverLayView and encountered a weird behavior. Before presenting the UIImagePickerController, I set allowsEditing to YES. After the capture screen comes up, I tap on a button that triggers takePicture(). Instead of presenting the editing screen, the delegate method didFinishPickingMediaWithInfo() gets called right away. Can anyone help me figure out what I could be doing wrong? I pasted some of my code below...
Thanks!
- (BOOL)shouldStartCameraController {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
return NO;
}
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float overlayOffset = 0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (screenSize.height > 480.0f) {
overlayOffset = 195;
} else {
overlayOffset = 103;
}
} else {
/*Do iPad stuff here.*/
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
&& [[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera] containsObject:(NSString *)kUTTypeImage]) {
cameraUI.mediaTypes = [NSArray arrayWithObject:(NSString *) kUTTypeImage];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceRear;
} else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
} else {
return NO;
}
UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cameraUI.view.frame.size.width, cameraUI.view.frame.size.height)];
UIImageView *cameraOverlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"iphone5_camera_overlay.png"]];
cameraOverlayImageView.frame = CGRectMake(0, 0, cameraUI.view.frame.size.width, cameraUI.view.frame.size.height);
[cameraOverlayView addSubview:cameraOverlayImageView];
UILabel *cameraLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0.0f, self.view.bounds.size.height-overlayOffset, self.view.bounds.size.width, 50.0f)];
[cameraLabel setTextAlignment:NSTextAlignmentCenter];
[cameraLabel setBackgroundColor:[UIColor clearColor]];
[cameraLabel setTextColor:[UIColor whiteColor]];
[cameraLabel setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.300f]];
[cameraLabel setShadowOffset:CGSizeMake( 0.0f, -1.0f)];
[cameraLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
[cameraOverlayView addSubview:cameraLabel];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton addTarget:self action:#selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchDown];
[cancelButton setFrame:CGRectMake(10, cameraOverlayView.frame.size.height-60, 50, 50)];
[cancelButton setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
[cameraOverlayView addSubview:cancelButton];
UIButton *snapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[snapButton addTarget:self action:#selector(takePictureButtonPressed:) forControlEvents:UIControlEventTouchDown];
[snapButton setFrame:CGRectMake(110, cameraOverlayView.frame.size.height-60, 100, 50)];
[snapButton setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
[cameraOverlayView addSubview:snapButton];
cameraUI.allowsEditing = YES;
cameraUI.showsCameraControls = NO;
cameraUI.delegate = self;
self.imagePickerController = cameraUI;
[self presentModalViewController:cameraUI animated:YES];
return YES;
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
[self shouldPresentPhotoCaptureController];
}
#pragma mark - UIBarButton Selectors
- (void)takePictureButtonPressed:(id)sender {
NSLog(#"takePictureButtonPressed...");
// TODO: take picture!
[self.imagePickerController takePicture];
}
Possible dublicate: How do I use [camera takePhoto] and edit using UIImagePicker - UIImagePickerController.allowsEditing = YES
According to takePicture: method in Apple documentation:
Use this method in conjunction with a custom overlay view to initiate the programmatic capture of a still image. This supports taking more than one picture without leaving the interface, but requires that you hide the default image picker controls.
Calling this method while an image is being captured has no effect. You must wait until the associated delegate object receives an imagePickerController:didFinishPickingMediaWithInfo: message before you can capture another picture.
It seems that this approach (custom overlay) it is configured in order to be managed by yourself. Even if "allowsEditing = YES" the taken picture will be directly sent to imagePickerController:didFinishPickingMediaWithInfo:.
Based on that if we want to edit the taken picture using our custom user interface we should create an according custom edit screen for that purpose.
How can i add the activity indicator beside the right bar button item (CreateNew button)?
One of the approaches that you can use is initializing the Bar Button using some custom view.
A bar minimum code which can help you get some hint is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
[aView setBackgroundColor:[UIColor blackColor]];
[[aView layer] setCornerRadius:8.0f];
[[aView layer] setBorderColor:[UIColor whiteColor].CGColor];
UIActivityIndicatorView* loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[loadView startAnimating];
[aView addSubview:loadView];
[loadView setFrame:CGRectMake(5, 5, 30, 30)];
[loadView release];
UIButton* aButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 2, 80, 35)];
[aButton setImage:[UIImage imageNamed:#"btnLogin.png"] forState:UIControlStateNormal];
[aView addSubview:aButton];
[aButton release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:aView];
[self.navigationItem setRightBarButtonItem:barButton];
[aView release];
[barButton release];
}
This is If you do it programmatically , else you can also make use of nib. Creating bar button this way will look something like this -
You can look for more option making using of [[UIBarButtonItem alloc] initWithCustomView:aView]; method.
Hope it helps!!
For this try to customize UINavigationController Class.
Add Activity Indicator on navigationBar and control it from your ViewController.
You can add the activity indicator using the following code:
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
Please refer this link for detailed code.
Airplay button is showing on the UIToolbar but when clicking it nothing is happening. It should open up airplay picker. but it is not.
UIButton *volumeView = [UIButton buttonWithType:UIButtonTypeCustom];
[volumeView addTarget:self action:#selector(MPVolumeView:) forControlEvents:UIControlEventTouchUpInside];
volumeView.frame = CGRectMake(0, 0, 40, 40);
UIImage *icon = [UIImage imageNamed:#"airplay-1.png"];
[volumeView setImage:icon forState:UIControlStateNormal];
UIBarButtonItem *volumeview = [[UIBarButtonItem alloc] initWithCustomView:volumeView];
-(void)MPVolumeView:(id)sender
{
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
[volumeView setShowsVolumeSlider:NO];
[volumeView setShowsRouteButton:YES];
// [volumeView sizeToFit];
[volumeView release];
}
I dont see anything wrong with the code but still wondering why it is not opening up airplay picker for scrollview images in the app. Actually in this case airplay is for uiimages. can we airplay UIImages.
Thanks for help.
Creating an instance of MPVolumeView doesn't actually open the Airplay picker, it creates the button which upon press, opens the picker.
Try something like this:
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 45, 33)];
myVolumeView.showsVolumeSlider = NO;
myVolumeView.showsRouteButton = YES;
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myVolumeView];
I am creating a sample app which allow use to capture image and record video with custom buttons.
I created an instance of UIImagePicker controller and assign delegate and all is done but takePicture, is not calling delegate method.
I refer few of post too UIImagePickerController takePicture not responding and UIImagepickerController [takepicture] modal view disappears no delegate callback but not getting my solution.
Here is my code. Please let me know if I am missing anything:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.hidden = YES;
imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setAllowsEditing:NO];
[imagePicker setMediaTypes:[NSArray arrayWithObject:#"public.movie"]];
[imagePicker setCameraCaptureMode:UIImagePickerControllerCameraCaptureModeVideo];
[imagePicker setShowsCameraControls:NO];
[imagePicker setToolbarHidden:YES];
[imagePicker setDelegate:self];
UIView *overlayView = [[UIView alloc] initWithFrame:[[imagePicker view]frame]];
[overlayView setBackgroundColor:[UIColor clearColor]];
UIButton *btnCapture = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnCapture setFrame:CGRectMake(0, overlayView.frame.origin.y + overlayView.frame.size.height - 35, 30, 30)];
[btnCapture setTitle:#"c" forState:UIControlStateNormal];
[btnCapture addTarget:self action:#selector(captureFrame:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btnStopVideo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnStopVideo setFrame:CGRectMake(40, overlayView.frame.origin.y + overlayView.frame.size.height - 35, 30, 30)];
[btnStopVideo setTitle:#"s" forState:UIControlStateNormal];
[btnStopVideo addTarget:self action:#selector(btnStopTapped:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:btnStopVideo];
[overlayView addSubview:btnCapture];
[imagePicker setCameraOverlayView:overlayView];
[self presentModalViewController:imagePicker animated:NO];
}
- (void)captureFrame:(id)sender {
[imagePicker takePicture];
}
But delegate method:- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info is not being called.
Please help me to solve this issue.
Thanks to all
Just add this line
picker.showsCameraControls = NO;
You need to make sure you implement the UIImagePickerControllerDelegate protocol in your header.
.h
#interface MyViewController <UIImagePickerControllerDelegate>
{
//
}
#end
You better to check if your UIImagePickerController.delegate still your controller.
I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.
How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.
You can do this in this way:
#property (nonatomic, strong) UITextField *textField;
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonDidPressed:)];
UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
[toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
self.textField.inputAccessoryView = toolbar;
}
- (void)doneButtonDidPressed:(id)sender {
[self.textField resignFirstResponder];
}
+ (CGFloat)toolbarHeight {
// This method will handle the case that the height of toolbar may change in future iOS.
return 44.f;
}
you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation,
It'll not only show the return key but also provide you with some extra punctuations
There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -
if (there is a touch outside of your textField)
{
[textField resignFirstResponder];
}
This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];
If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.
What you do want to do to simulate a return key is to actually add it like this:
Register for a keyboard did show notification and add this to the code:
if ([self.passCodeLabel isFirstResponder])
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
//doneButton.frame = CGRectMake(0, 163, 257, 257);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"doneup.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"donedown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
NSLog(#"%#",[[UIApplication sharedApplication] windows]);
UIView* keyboard;
NSLog(#"Shared applicaiton windows count:%i",tempWindow.subviews.count);
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(#"%#",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES)
{
NSLog(#"Adding return button");
[keyboard addSubview:doneButton];
}
}
}
This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).
Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.
Create the button
Look for the keyboard subview
Add the button to that subview
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
_txt_mobileno.inputAccessoryView = numberToolbar;
}
-(void)doneWithNumberPad
{
[_txt_mobileno resignFirstResponder];
}