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];
}
}
Related
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];
}
}
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;
}
}
I Would create a app with camera but when i write this code in ImageViewController.m
xcode: EXPECTED METODY BODY and "."
how i can fix this?
thanks
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
It says you have not method at all. Try changing to this (note the braces after method name and after all code):
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
}
Can You Try this.
- (IBAction)presentPicker:(id)sender {
// **********************************************
// * Show action sheet that will allow image selection from camera or gallery
// **********************************************
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Image from Camera", #"Image from Gallery", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
#pragma mark - Image picker methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
switch (actionSheet.tag) {
case 1:
switch (buttonIndex) {
case 0:
[self showCameraImagePicker];
break;
case 1:
[self showGalleryImagePicker];
break;
}
break;
default:
break;
}
}
- (void)showCameraImagePicker {
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Simulator" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
#endif
}
- (void)showGalleryImagePicker {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
photo.image = image;
//[self presentImageCropperWithImage:image];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:NO completion:nil];
// Extract image from the picker
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
photo.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
// [self presentImageCropperWithImage:image];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I just want the user to take a photo in app→crop→save. So I would want to use the allowsEditing property of UIImagePickerControl. However I don't know how i can do it. this is my code until now for camera.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
Use this for pick photo from camera
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
use this for pick photo from photo library
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
use following two delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Add this in your ViewDidLoad method, so if your device is not support camera then display this alert message.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
Add both protocols to the AppViewController.h file:
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
I have the method, that take photos from gallery or from the camera
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
But when i run it on the simulator, code doesnt work. And it doesnt work in picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum and picker.sourceType = UIImagePickerControllerSourceTypeCamera
Is the problem in the simulator or in the code?
Try this,
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
else
{
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self.navigationController presentModalViewController:picker animated:NO];
If you are creating the app for iPad. You will have to present the gallery in a popOver control.
Swift 3/4/5 verison:
if UIImagePickerController.isSourceTypeAvailable(.camera) {
picker.sourceType = .camera
}
else {
picker.sourceType = .savedPhotosAlbum // or .photoLibrary
}
Swift 2 version:
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
picker.sourceType = .Camera
}
else {
picker.sourceType = .SavedPhotosAlbum // or .PhotoLibrary
}
In simulator, you can't use cameraCaptureMode and showsCameraControls.
In simulator your picker.sourceType = UIImagePickerControllerSourceTypeCamera wont be called as there is no camera available in simulator. Also its a good practice to check whether the source type is available to avoid crashes.
#import <MobileCoreServices/UTCoreTypes.h>
….
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
imagePickerCamera.delegate = self;
imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePickerCamera.allowsEditing = YES;
imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerCamera animated:YES completion:nil];
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
imagePickerAlbum.delegate = self;
imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePickerAlbum.allowsEditing = YES;
imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerAlbum animated:YES completion:nil];
}
Similarly to the above answers, but I found this easier. Show a pop up alert if the device doesn't have a camera (like the simulator). Sam code, different usage:
//button if to take a photo
- (IBAction)takePhoto:(id)sender {
//checks if device has a camera
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"You don't have a camera for this device" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
//shows above alert if there's no camera
[noCameraAlert show];
}
//otherwise, show a modal for taking a photo
else {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:NULL];
}
}