Code crash when I call the camera - ios

This is the code that I have:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing=YES;
[self presentViewController:picker animated:YES completion:nil];
}
PS: "sourceType = UIImagePickerControllerSourceTypePhotoLibrary " It's ok.
"sourceType = UIImagePickerControllerSourceTypeCamera " It's wrong.
This is the error that I get:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x21f1949f 0x2f700c8b 0x21e39873 0x21e39657 0x2544bbc3 0x2544b815 0x253e4d67 0x2aa06681 0x253d924f 0x24e01a0d 0x24dfd3e5 0x253eb987 0x26bd5fa5 0x26bd13b3 0x26bd2429 0x26bcbad9 0x2aa03cb5 0x2a9d1b53 0x253f151b 0x2548528f 0x25484d63 0x256cba3f 0x256caf87 0x254f92cf 0x256ca81f 0x256bc6df 0x254f919d 0x2561fb1b 0x2561f9f3 0x253f151b 0x256a7c8d 0x25455a25 0x253d182b 0x21edfd95 0x21edd453 0x21edd85b 0x21e2b3c1 0x21e2b1d3 0x291e90a9 0x2543afa1 0x1d1a65 0x2fc80aaf)
Can I get some help to solve this?

-(void)actionLaunchAppCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Camera Unavailable"
message:#"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
}

Related

can I capture 5 consecutive pictures in 1 second just one click in objective c

I am using the below code for clicking 5 consecutive images in just 1 click but i am unable to do this. what should I do?
- (IBAction)cameraaBtton:(id)sender
{
for ( int i=0;1<5; i++)
{
[self CapturePhoto];
}
}
and the capture photo method is:
- (void)CapturePhoto
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:NULL];
}
else
{
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:#"Device has no camera" message:#"Dear user please buy a apple cell" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil];
[alertview show];
}

Camera is not working in IOS

I have implemented the following and camera is opened but app is crashed and sometime when open and take the pictures then app is crasshed and log only show "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates Received memory warning."
The same functionality I have used for take the image from gallery and its working.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{
if (buttonIndex==0) {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Device has no Camera!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}else{
[self performSelector:#selector(loadCamera) withObject:nil afterDelay:1.0];
}
}
}
-(void)loadCamera{
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
}
Can anyone help.
Solved it by following code:
-(void)loadCamera{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
dispatch_async(dispatch_get_main_queue(), ^{
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion: nil];
});
}
}
This code worked for me with iOS 9.2 and xCode 7.2
- (IBAction)takeAPhoto:(VSButton *)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}else {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
[self performSelector:#selector(loadCamera) withObject:nil afterDelay:1.0];
-(void)loadCamera
{
if ([UIIagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera)
{
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
}
}

ipad camera open view issue

i have an issue in my camera view, which is not opened when shoot from iPad. When click on camera button it have do nothing, please anyone help me. I am using xcode 6.4 and OS installed on iPad is 8.4.1
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Camera Unavailable"
message:#"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
Try this code.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
And add MobileCoreServices into your framework.

UiimagepickerView: App crashed when presenting the imagepickerview?

I have following code for the image picker:
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
switch (popup.tag) {
case 1: {
switch (buttonIndex) {
case 0:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
break;
case 1:
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
break
And while presenting following error appears:
[__NSCFArray pointSize]: unrecognized selector sent to instance 0x6a5e640
2014-12-26 12:04:50.304 AssetDB[78872:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray pointSize]: unrecognized selector sent to instance 0x6a5e640'
*** First throw call stack:
(0x134f052 0x1903d0a 0x1350ced 0x12b5f00 0x12b5ce2 0x30168b 0x310aae 0x3134d7 0x3133dd 0x313423 0x313e88 0x2d64b0 0x3137f7 0x2db322 0x1350e72 0xfa92d 0x104827 0x104922 0x2d4f47 0x3710b6 0x2d64b0 0x371087 0x370dbd 0x373b32 0x373b97 0x36539c 0x3727fc 0x2d86ec 0x2d4c72 0x2d9a4f 0x2d372b 0x341116 0x5804c7 0x369427 0x36958c 0x8fc2 0x65aa1f 0x1350ec9 0x29f5c2 0x29f55a 0x344b76 0x34503f 0x3442fe 0x2c4a30 0x2c4c56 0x2ab384 0x29eaa9 0x223cfa9 0x13231c5 0x1288022 0x128690a 0x1285db4 0x1285ccb 0x223b879 0x223b93e 0x29ca9b 0x29a8 0x2905)
terminate called throwing an exceptionkill
It's a bit old but recently I have had a similar problem.
Check if you have somewhere
NSFontAttributeName: "Some string"
this should be replaced with
NSFontAttributeName: UIFont(name: "Some-font", size: yourSize)
This worked for me.
- (IBAction)OnClickbutton:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: (id)self
cancelButtonTitle: #"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: #"Take a new photo", #"Choose from existing", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Take a new photo"]) {
[self takeNewPhotoFromCamera];
}
if ([buttonTitle isEqualToString:#"Choose from existing"]) {
[self choosePhotoFromExistingImages];
}
}
- (void)takeNewPhotoFromCamera
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.editing = YES;
imagePickerController.delegate = (id)self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)choosePhotoFromExistingImages
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = (id)self;
controller.navigationBar.barStyle = UIBarStyleBlackTranslucent; // Or whatever style.
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}

Presenting the UIImagePickerController causes a crash on iOS 7

I'm having an issue presenting the UIImagePickerController on iOS 7 devices. I use the following code to present the image picker.
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraUI.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
cameraUI.delegate = self;
[[self presentViewController:cameraUI animated:YES completion:NULL];
After the call to presentViewController, the application crashes due to an exec bad access. The console reports the following exceptions.
[SBSAccelerometer valueRestriction]: unrecognized selector sent to instance 0x1650e360
[__NSCFNumber valueRestriction]: unrecognized selector sent to instance 0x146d0e70
I enabled zombies to see if an object is getting deallocated prematurely. Zombies reports the following exceptions:
[NSISRestrictedToNonNegativeVariable retain]: message sent to deallocated instance 0x156f0010
Any thoughts?
EDIT
Here is the stack trace I receive with zombies enabled:
This is a bug in iOS 7 on iPad. It appears the solution for now is to request permission to photos before opening the UIPopoverControl. Here is how I implemented my solution:
**// Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
void(^blk)() = ^() {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (NIIsPad()) {
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromBarButtonItem:self.popoverAnchor permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self.navigationController presentModalViewController:picker animated:YES];
}
};
// Make sure we have permission, otherwise request it first
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAuthorizationStatus authStatus;
if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
authStatus = [ALAssetsLibrary authorizationStatus];
else
authStatus = ALAuthorizationStatusAuthorized;
if (authStatus == ALAuthorizationStatusAuthorized) {
blk();
} else if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
[[UIAlertView alertViewWithTitle:#"Grant photos permission" message:#"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
} else if (authStatus == ALAuthorizationStatusNotDetermined) {
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Catch the final iteration, ignore the rest
if (group == nil)
dispatch_async(dispatch_get_main_queue(), ^{
blk();
});
*stop = YES;
} failureBlock:^(NSError *error) {
// failure :(
dispatch_async(dispatch_get_main_queue(), ^{
[[UIAlertView alertViewWithTitle:#"Grant photos permission" message:#"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
});
}];
}
}**
Don't forget to add AssetsLibrary.framework to your project.

Resources