In my app I want the user to be able to take a picture or use one from the photo library. When the user clicks the button I made a alert view pops up at the user can choose between taking a new photo or one from the photo library. Here is the code I've used:
- (void)PictureAlert:(id)sender {
UIAlertView *AlertDialog;
// Setting up AlertDialog.
AlertDialog = [[UIAlertView alloc] initWithTitle:nil
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Choose From Library", #"Take New Picture", nil];
[AlertDialog show]; }
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([ButtonTitle isEqualToString:#"Choose From Library"]) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// Pick photo.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
} else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
}
} else if ([ButtonTitle isEqualToString:#"Take New Picture"]) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Take new photo.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.wantsFullScreenLayout = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
} else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
}
}
}
The problem is that if the user wants to take a new picture the camera interface pops up, and then if you rotate the device the interface looks like this:
And then when the user rotate it back it suddenly looks like this:
A little side problem is that the camera takes a long time to load.
Any thoughts would be appreciated :)
A few things you might want to consider:
Setting the wantsFullScreenLayout property to YES will cause the view to ignore the status bar. But since you are using the default camera controls, the status bar hides automatically. This is the most likely cause for the 20 pixel grey area on the bottom of the image.
The default camera controls are designed to be in portrait mode only. Since your first image looks like you somehow rotated the screen, you should look into your code (probably shouldAutoRotate) and see why you are rotating the view like that. This should fix the problem of the zoom you are getting in your landscape picture.
You will have memory leaks if you create a UIImagePickerController, present it, and then have no reference to it to release it later. I would recommend setting the UIImagePickerController in the interface, and setting it up in the viewDidLoad method. Try:
.h
#interface yourView:UIViewController <UIImagePickerControllerDelegate> {
UIImagePickerController * picker;
}
.m
- (void)dealloc; {
[picker release];
[super dealloc];
}
- (void)viewDidLoad; {
[super viewDidLoad];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if([ButtonTitle isEqualToString:#"Choose From Library"]){
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
else{
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
[AlertDialog release];
}
}
else if([ButtonTitle isEqualToString:#"Take New Picture"]){
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
else{
// Setting up AlertDialog.
UIAlertView *AlertDialog;
AlertDialog = [[UIAlertView alloc] initWithTitle:#"Error accessing camera"
message:#"Device does not support a camera"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[AlertDialog show];
[AlertDialog release];
}
}
}
This should clean-up the memory leaks, and improve the load time. Hope that Helps!
Some time its happened if you are using a old generation iphone that have current os for example you are having iphone 3G and you update its ios to ios5 then some of the app you installed can behave differently you can check your app to another device for rectify your problem.
Make sure you set the view controller hierarchy, with mainWindow.rootViewController and [vc addChildViewController:]. This propagates orientation information down to where you need it.
It looks like for my project this was happening because you have not written the shouldAutoRotateToInterface: method in your root view controller. The rotate message propagates all the way down to the root view controller's shouldAutoRotateToInterface delegate when UIImagePickerController is called. Your method should look like this:
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
When I upgraded my project to iOS 5, I borrowed my root view controller from an iOS 3 project. iOS 3 did not automatically write this method in view controller classes Try it out and let me know.
Related
how to open camera by clicking tab in tab bar application and i want to also custom that camera controller? i have seen many apps that do like this and they have extra functionalities also like instagram this is also used like this pinterest this is another picplz i have googled some links but i didn't get the perfect result i have tried to put open camera via - (void)viewWillAppear:(BOOL)animated but it is not working perfect can any body can guide me to create camera view like above some applications?
i have seen this apple example. for custom view of camera i have seen this.
hey anybody have any answers?
Try this code snippet.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
}
else
{
// Put alert no camer found
}
[picker release];
- (IBAction)buttonTap:(id)sender
{
UIActionSheet * theActionSheet;
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
theActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", nil) destructiveButtonTitle:NSLocalizedString(#"Choose existing photo", nil) otherButtonTitles:nil, nil, nil];
self.actionSheet = theActionSheet;
}
else
{
theActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", nil) destructiveButtonTitle:NSLocalizedString(#"Take photo", nil) otherButtonTitles:NSLocalizedString(#"Choose existing photo", nil), nil, nil];
self.actionSheet = theActionSheet;
}
self.actionSheet.tag = 100;
self.actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[self.actionSheet showInView:self.view.window]; // show from our table view (pops up in the middle of the table)
[theActionSheet release];
}
I am developping an App using XCode 4.2 that detects a QR code.
I am trying to make a switch view after QR code detection but it is not working at all
here is the code am using :
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
// ADD: get the decode results
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
break;
NSString *string=symbol.data;
NSString *string2=#"1234";
if ([string isEqualToString:string2]) {
//this is the part that is not working : it doesn t load the AboutView at all
AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:about animated:YES];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"This is not a recognized QR code!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissModalViewControllerAnimated: YES];
}
thanks
The issue is that the reader is the presented view controller in zbar's example code
-(void)presentReaderInViewController:(UIViewController*)vc
and you are treating self as if it is presented
You should use reader to present your AboutView and only dismiss reader in the else block
if ([string isEqualToString:string2]) {
//this is the part that is not working : it doesn t load the AboutView at all
AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
[reader presentModalViewController:about animated:YES];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"This is not a recognized QR code!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissModalViewControllerAnimated: YES];
}
You might also want to wait to dismiss reader in the delegate method of your alert view (create a soft reference and dismiss that... myReader = reader; when you set up the alertview)
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
[myReader dismissModalViewControllerAnimated: YES];
}
I can't seem to find an answer for this, or maybe can't understand the things people wrote over the web...
I have a UITabBar with 3 tubs.
One of the tabs is a UIImagePickerController. This TabBar Item is connected to a view controller that i set also as the delegate for the Image picker (camera).
I want that then someone take a photo or press cancel, The first TabBar item will be choosen (don't want to stay in the TabBar that holds the camera).
My question is, How do I "talk" with the TabBar controller from a view controller that is in one of the TabBar items?
my code in the TakePhotoViewController.m file that is in the 3rd TabBer item and i want to go the first item.
-(void) viewWillAppear:(BOOL)animated{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
and the delegate methods:
#pragma mark -
#pragma mark - UIImagePicker delegate methods
//saving the image that was taken
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
Thank you very much,
Erez
[self.tabBarController setSelectedIndex:1];
You can pass your desired tab index value instead of 1.
i have created a project which has different xib viewcontrollers.In first view am selecting an image through picker controller and am displaying it in the secondviewcontroller.In secondview controller i have some buttons and i have given some IBActions to them.Here starts my problem that am successfully displaying the image in secondviewcontorller but when i tap on button in that viewcontroller app is terminating and the debugger showing the error message as program terminated due to uncaught exception
Here is the code:
To select the pic through pickercontroller in first view
-(IBAction)btnChoosePicClicked {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Error accessing photo library" message:#"Device does not support a photo library" delegate:nil cancelButtonTitle:#"Drat!" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
displaying in the second view and assigning the actions .h file
#interface editScreen : UIViewController{
IBOutlet UIButton *btnRotate;
IBOutlet UIButton *btnLibrary;
IBOutlet UIImageView *imgView;
int RotateAngle;
}
-(void)setImage:(UIImage *)img;
-(IBAction)btnLibraryClicked;
-(IBAction)RotateImage;
#end
.m file
#implementation editScreen
-(void)setImage:(UIImage *)img
{
[imgView setImage:img];
imgView.userInteractionEnabled = YES;
}
-(IBAction)RotateImage
{
CGAffineTransform transform = imgView.transform;
transform = CGAffineTransformRotate(transform, M_PI/2);
imgView.transform=transform;
RotateAngle+=90;
if(RotateAngle>=360)
{
RotateAngle-=360;
}
//imageview.transform = CGAffineTransformScale(imageview.transform, -1.0, 1.0);
}
-(IBAction)btnLibraryClicked {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
// NSFileHandle *fileHandle = [[NSFileHandle alloc]initWithFileDescript
}
else
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Error accessing photo library" message:#"Device does not support a photo library" delegate:nil cancelButtonTitle:#"Drat!" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
I dont know whats wrong with my code please help me.I have given appropriate connections in IB..
Thanks in Advance
For some UIKit class, his delegate has some methods which must be implemented.
For example, UIImagePickerViewControllerDelegate must implement following methods:
– imagePickerController:didFinishPickingMediaWithInfo:
– imagePickerControllerDidCancel:
For your above codes, I don't know whether these methods are implemented or not. Hope these information can help you.
I used to show a splash screen which in background load some data from web, I also check that if the location of the user is changed from one city to another city I want to show in alert to the user with the message that you are now in "CityName" would you like to see data from this city?
I have tabbed application and I have presented the splash screen as follow in the app delegate class.
SplashViewController *controller = [[SplashViewController alloc] initWithNibName:nil bundle:nil];
tabBarController.view.frame = [[UIScreen mainScreen] bounds];
controller.tabBarController = self.tabBarController;
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[window addSubview:controller.view ];
//[window addSubview:tabBarController.view ];
[self.tabBarController presentModalViewController:controller animated:YES];
[window makeKeyAndVisible];
[controller release];
Now when I show the alert screen it crash the application with "EXC_BAD_ACCESS" message and the stack trace show that _buttonClick is released in UIAlertView class.
Please advise what should I do, I also tried with UIActionSheet but the same problem with this thing too.
I think there is some problem with the model thing with the current view (SplashView).
Thanks in advance.
Are you trying to display your UIAlertView inside of your SplashViewController viewDidAppear? If not, I would try that first. I would also make sure you have your UIAlertView clickedButtonAtIndex method setup properly to try and trap what is going on.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Your Location Has Changed"
message:#"Your location has changed since you last opened opened THEAPP. Would you like to refresh your data?" delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.tag = 1;
[alert show];
[alert autorelease];
Then for the clickedButtonAtIndex method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//--NSLog(#"You clicked button #%i",buttonIndex);
if (alertView.tag == 1){
if (buttonIndex == 0) {
//--NSLog(#"CANCEL");
} else if (buttonIndex == 1) {
//--NSLog(#"OK");
}
}
}
Doing all of this on a splash screen should be fine as long as you take into account the HIG's requirements for using the users location. Hope this helps!
I resolve this issue, the problem was that my Splash View was a modeled view and invoked by
[self.tabBarController presentModalViewController:controller animated:YES];
what I did that I shifted the data downloading to another view controller and there I can show alerts, and can handle that