UIImagePickerController causes crash on ios5 - iphone

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UINavigationController *HomeNav = [[UINavigationController alloc] initWithRootViewController:[[HomeController alloc] init]];
[HomeNav.navigationBar setTintColor:[UIColor clearColor]];
CustomTabBar *tabBar = [[CustomTabBar alloc] init];
tabBar.buttonImages = [NSArray arrayWithObjects:#"t1.png" , nil];
tabBar.hightLightButtonImages = [NSArray arrayWithObjects:#"th1.png", nil];
tabBar.viewControllers = [NSArray arrayWithObjects:HomeNav , nil];
self.tabBarController = tabBar;
[tabBar release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
in HomeController view, there is one button
if tap the button I call `AViewController.
-(IBAction)tapButtonA:(id)sender;
{
[self.navigationController pushViewController:AViewController animated:YES];
}
there is also a button on the view of AViewController.
if I tap the button, I call UIImagePickercontroller
-(IBAction)tapButtonB:(id)sender;
{
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
[self presentModalViewController: picker animated:NO];
}
If I tap the cancel button of the UIImagePickerController
-(void)imagePickerControllerDidCancel:(UIImagePickerController*)picker{
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
the UIImagePicker will dismiss, but after 1,2 seconds the app crashes and displays
Welcome any comment

Following code is solve your problem,
#pragma mark - UIActionsheet Delegate method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.allowsEditing = NO;
self.imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentModalViewController:self.imagePicker animated:YES];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to connect camera."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
break;
case 1:
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentModalViewController:self.imagePicker animated:YES];
break;
default:
break;
}
}
#pragma mark - UIImagePicker Delegate method
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imgForEvent=image;
// // Save image
// if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
// }
[self dismissModalViewControllerAnimated:YES];
}
- (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];
[self.imagePicker dismissModalViewControllerAnimated:YES];
}
This code may helping to solveing your problem

I think you want:
-(void) imagePickerControllerDidCancel:(UIImagePickerController*)picker {
[picker.presentingViewController dismissModalViewControllerAnimated:YES];
}

try this
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}

Related

UIPopoverController How to fix for iPad?

I am getting an error / crash on iPad only when using the following code, iPhone works fine.
It is basically when trying to load the gallery to pick an image.
The debug error is;
"On iPad, UIImagePickerController must be presented via UIPopoverController'"
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
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:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing Camera"
message:#"Device does not support a Camera"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"the info opf picker is %#",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(#"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(#"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,#"Image",#"Theme",#"Cell_Text",nil];
// NSLog(#"the custom array%# and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(#"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(#"size of myObject: %zd", malloc_size(myCustomTableView));
}
Could anyone be so kind as to help with the code above ? I am guessing an if/else statement but have not the first idea of what it would be and where to place it.
Thanks in advance,
Chris
EDIT - Ok I now have it working the load from gallery now loads 'Photos' in a small window, where I can select, however there is no 'Select' or 'Choose' button, if I click the photo, there is no tick, to say it is selected, however it does select it, but to dismiss I click outside the window... So it works, but I am guessing I have missed something out, here is the code;
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if(popoverController != nil)
{
[popoverController release];
popoverController = nil;
}
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
popoverController = popover;
popoverController.delegate = self;
[popover presentPopoverFromRect:CGRectMake(0, 0, 200, 800)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
else
{
[self presentModalViewController:picker animated:YES];
}
// [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:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing Camera"
message:#"Device does not support a Camera"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"the info opf picker is %#",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(#"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(#"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,#"Image",#"Theme",#"Cell_Text",nil];
// NSLog(#"the custom array%# and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(#"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(#"size of myObject: %zd", malloc_size(myCustomTableView));
}
-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
The error is very clear:
On iPad, UIImagePickerController must be presented via UIPopoverController'
You need to put the image picker in a popover controller when run on the iPad. This is true when the sourceType is for the photo library. The camera based image picker can be shown as a full screen modal view controller.
This isn't an issue on the iPhone because the iPhone doesn't support the UIPopoverController.
The sample app PrintPhoto demonstrates how to show the image picker in a popover. Specifically, see PrintPhotoViewController.m.
Do something like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//present as a pop over vc
} else {
//present as a modal vc
}
To present a popover you might use this method
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
You can read the documentation at this link link

iPhone camera not showing up

I am having awkward problem in using the ios camera from a action sheet: When the user touches the "picture button" an action sheet shows up with two options (to use a photo from the photo library or to take a picture with the camera).
Whatever option I choose, nothing happens, but when I select the media type again, it works.
Bellow is my code:
- (IBAction)selectMediaType: (id)sender {
[appDelegate hideTabBar];
UIActionSheet *action = [[UIActionSheet alloc]
initWithTitle: nil
delegate:self
cancelButtonTitle:#"Fechar"
destructiveButtonTitle: nil
otherButtonTitles:#"Galeria", #"Tirar Foto", nil];
[action showFromTabBar: appDelegate.tabController.tabBar];
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePicker.delegate = self;
imagePicker.hidesBottomBarWhenPushed = YES;
imagePicker.mediaTypes = mediaTypes;
imagePicker.allowsEditing = NO;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
imagePicker.hidesBottomBarWhenPushed = YES;
} else if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePicker.delegate = self;
imagePicker.mediaTypes = mediaTypes;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[self presentModalViewController:imagePicker animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#""
message:#"Your device does not support this feature!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
} else {
[appDelegate showTabBar];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image;
NSURL *mediaURL;
mediaURL = (NSURL *) [info valueForKey:UIImagePickerControllerMediaURL];
if (mediaURL == nil) {
image = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
}
imageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
[appDelegate showTabBar];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
[appDelegate showTabBar];
}
Could anyone help me with this?
Thanks.
I got it.
Basically it was a memory leak issue: I was switching views adding subviews in the super view (not in the current view) but I was not removing the old one from the super view, so I got a memory leak ;)

How to take picture from Camera & saved in Photo Gallery by programmatically?

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. Is it possible in iPhone Simulator?
so please tell me any link or materials you have.
Thanks in advance
You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
More info on image:didFinishSaving.... can be found here
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
Also have a look at this post
https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
it is not possible in simulator.You have to use the device.
I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. You can definitely use a device.... You can make use of UIImagePickerController for capturing images.
There are 3 prerequesites needed to take a picture programmatically:
The device needs to have a camera check by
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
Camera controls need to be hidden (picker is a UIImagePickerController)
picker.showsCameraControls = NO;
use the takePicture from UIImagePickerController
[picker takePicture];
Side note because it did bug me for a few minutes, the takePicture method also dismisses the view.
Also posted a more complete answer here Other Stack answer

Receive memory warning after 10 or 11 times capturing image using UIImagePickerControllerSourceTypeCamera in iphone

Hi I receive memory warning whenever I am using camera.
The error is like this:
"Receive memory warning..."
And the code is:
-(void) getPhoto{
GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
///////////////////////////////////photolibrary//////////////////////////////
if([appDelegate.photoselection isEqualToString:#"User Pressed Button 1\n"])
{
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Camera//////////////////////////////
else if([appDelegate.photoselection isEqualToString:#"User Pressed Button 2\n"])
{
#try
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
#catch (NSException * e)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"ALERT"
message:#"Please try again"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"ok", nil];
[av show];
}
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Cancel//////////////////////////////
else if([appDelegate.photoselection isEqualToString:#"User Pressed Button 3\n"])
{
if(appDelegate.sound == 1)
[classObj ButtonSound];
return;
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
How can i handle this?please help after taking picture i crop the image and save in application like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
[picker dismissModalViewControllerAnimated:YES];
imageView.image = image;
CGSize size = [imageView.image size];
CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);
NSValue *cropRectValue = [editingInfo objectForKey:#"UIImagePickerControllerCropRect"];
cropRect = [cropRectValue CGRectValue];
appDelegate.slectedimage = image;
imageView.hidden = YES;
if( [appDelegate.Name length] != 0 && max_att == 15)
{
btnNotDone.hidden = YES;
btnDone.enabled = YES;
}
//IMAGE SAVE IN DOCUMENTS//////
[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:#"/Documents"]]);
}
Please help me. I want to remove all warnings.
You're leaking the UIImagePickerController. Autorelease it on creation or release it after dismissModalViewControllerAnimated.
You may still get memory warnings, photos can be enormous, especially on the iPhone 4 and at a certain point you have two of them in memory: the UIImage and the autoreleased PNG.
P.S. you don't seem to be using size and cropRect so you could delete them.
Release your alert view. In general release any object you alloc. In case you have a retain property then assign a auto release object to it.
When you get a memory warning your view controller method - (void)didReceiveMemoryWarning is called. Here you will have to release any unwanted objects which you have cached. Typically that would be some images, views in stack etc.
Also check if you are having appropriate dealloc for objects in your modal view controller.
Are you implementing -imagePickerController:didFinishPickingMediaWithInfo:? The method you've implemented has been deprecated. You should use the other method even for images. What are you doing with the recorded videos?
On a side note, the following code –
#try
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
#catch (NSException * e)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"ALERT"
message:#"Please try again"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"ok", nil];
[av show];
}
should be
if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"ALERT"
message:#"Camera isn't available"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"ok", nil];
[av show];
[av release]
}
Now smarter thing would be to disable button 2 which would be
if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
button2.enabled = N0;
}

How to determine whether cameraDevice is available?

In my current application I have allowed the user to submit an image to an image service online. I allow the user to select from either their Photo Album or take a picture with the Camera.
However, I have an issue. If the device that is being used doesn't have a camera and the user selects to take a photo, the application crashes. I need to be able to determine whether or not the device has the ability to use the cameraDevice.
Below is my current code for presenting an UIActionSheet which allows the user to select the different options.
#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:#"" delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose An Existing Photo", #"Take A Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
[sheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the
NSLog(#"Album");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
} else if (buttonIndex == 1) {
NSLog(#"Camera");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
Thanks in advance!
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(#"No camera!"); }
Something like this also works:
NSString* b1 = #"Get from album";
NSString* b2 = nil;
BOOL cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if ( cameraAvailable ) {
b2 = #"Take a photo";
}
UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: alertTitle
delegate: self
cancelButtonTitle: #"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: b1, b2, nil];
[sheet showInView: self.view];
[sheet release];
I won't forget, I had to add tags and a new property, a UIActionSheet called sheet.
The following code is how I got everything to work, absolutely seamlessly.
#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc]
initWithTitle:#"" delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose An Existing Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
sheet.tag = 0;
[sheet release];
}
else {
sheet = [[UIActionSheet alloc]
initWithTitle:#"" delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose An Existing Photo", #"Take A Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
sheet.tag = 1;
[sheet release];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (sheet.tag) {
case 0:
if (buttonIndex == 0) {
//Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the
NSLog(#"Album");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
break;
case 1:
if (buttonIndex == 0) {
//Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the
NSLog(#"Album");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
} else if (buttonIndex == 1) {
NSLog(#"Camera");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
break;
}
}
I got a better solution; ordering button title and clear code.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (isCameraAvailable) {
[actionSheet addButtonWithTitle: NSLocalizedString(#"Take New Photo", #"")];
}
[actionSheet addButtonWithTitle: NSLocalizedString(#"Choose from Library", #"")];
[actionSheet addButtonWithTitle: NSLocalizedString(#"Cancel", #"")];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons -1;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView: self.view];
I hope my answer is helpful to your problem.