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];
}
}
Related
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];
}
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.
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];
}
}
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 been trying this for the past 2 days and I am not able to figure out the answer. I have searched all over and I haven't found the answer.
The question is I have a button which brings up the camera in my app(to take photos only). The camera opens up, but when i take a picture and click on "USE"(which is displayed at the bottom right) its crashing. Also, when the camera opens up, before taking a picture when I click "Cancel" it again crashes.
I tried using breakpoints and found out that, When I click on the "USE" button, it crashes in this line
[picker dismissViewControllerAnimated:YES completion:Nil]
I'm testing it in my iPad (iOS6).
Here is the Button Code here :
-(IBAction)getAlbum:(id)sender {
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]; //Since [Modal](http://stackoverflow.com/questions/12445190/dismissmodalviewcontrolleranimated-deprecated) has been removed
[self presentViewController:picker animated:YES completion:Nil];
//[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];
}
}
imagePickerController Method here:
- (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);
}
selectedLogoImg.image=photoTaken; //selectedLogoImg is the imageView
[self.clipartItemView addSubview:selectedLogoImg]; // To detect touch and move it I place it as a subview of self.clipartItemView
}
//[picker dismissModalViewControllerAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:Nil]
//[picker release];
//[picker dismissViewControllerAnimated:YES completion:^{
// NSLog(#"Dismiss completed");
//}];
}
didFinishSavingWithError Code Here:
- (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];
}
}
imagePickerControllerDidCancel Code Here:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//[picker dismissModalViewControllerAnimated:YES];
/*[picker dismissViewControllerAnimated:YES completion:^{
[self.view sendSubviewToBack:cardGalleryView];
}];*/
[picker dismissViewControllerAnimated:YES completion:Nil];
}
You should send the dismissViewControllerAnimated:completion: message to the view controller, not the picker. Try:
[self dismissViewControllerAnimated:YES completion:nil];
The above method is only for iOS 6. You need to use [self dismissModalViewControllerAnimated:YES] for iOS 5 and below.
Take a look at the description of the method in the documentation:
Dismisses the view controller that was presented by the receiver.
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, however, it automatically forwards the message to
the presenting view controller
This problem is due to your UIImagePickerController *picker object . ViewController isn't able to identify your picker object reference out of the getAlbum method scope.
1.> you can create UIImagePickerController object in your .h file
#interface yourViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate>
{
UIImagePickerController *picker;
UIPopoverController *popover;
}
and in .m file you just use it inside getAlbum IBAction method
-(IBAction)getAlbum:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIButton *btn=(UIButton *)sender;
if ([popover isPopoverVisible])
{
[popover dismissPopoverAnimated:YES];
popover=nil;
}
picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(btn.frame.size.width,btn.frame.size.height/2,1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
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];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo
{
myimage = [myimage fixOrientation];
[picker dismissModalViewControllerAnimated:YES];
[popover dismissPopoverAnimated:YES];
}
I hope it helps you to better understand.