My application crashes when running the following on iOS7.
Its was working perfectly fine on iOS6
UIImagePickerController *ImagePicker;
#try {
ImagePicker = [[[UIImagePickerController alloc]init]autorelease];
ImagePicker.sourceType= UIImagePickerControllerSourceTypeCamera;
ImagePicker.showsCameraControls = NO;
ImagePicker.toolbarHidden = YES;
ImagePicker.navigationBarHidden= YES;
// ImagePicker.wantsFullScreenLayout = YES;
ImagePicker.cameraViewTransform = CGAffineTransformScale(ImagePicker.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
}
#catch (NSException *exception) {
[ImagePicker release];
ImagePicker = nil;
}
#finally {
if(ImagePicker)
{
[cameralayer addSubview:[ImagePicker view]];
[cameralayer release];
}
}
You are not guaranteeing that ImagePicker will be in memory by the time you reach the line that adds it's view to the cameralayer. Don't autorelease ImagePicker right after you create it. Only release it once you are really done using it and its view.
Related
Edit - I solved this myself - see the notes at the bottom
When using iOS7 on Xcode 5, I am using an option to take an image from a camera, or from the photo library, once the image is chosen (or a new picture taken) the view should flip over to the next screen.
This does not happen on the iPhone running iOS7, it works fine on the iPad, but the method is slightly different, but it does appear to be iPhone only problem on iOS7.
here is the code used, for example, on the choose image from library function;
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
[self presentModalViewController: cameraUI animated: YES];
}
}
Also, the code once picker is finished;
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
//Disable buttons
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self disableButtons];
//Get image
self.originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
//Dismiss
if(_popover)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[_popover dismissPopoverAnimated:YES];
_popover = nil;
}
}
else
[picker dismissModalViewControllerAnimated: YES];
//Next
[self performSelector: #selector(nextScreen) withObject:nil afterDelay:0.5];
}
I fixed this by switching out;
[picker dismissModalViewControllerAnimated: YES];
With
[picker dismissViewControllerAnimated:NO completion:nil];
First you need to make sure that logic reaches the correct place it's intended to, try setting a breakpoint or NSLog before the iPhone's specific line, try this (You also missed curled braces, added them here) :
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
NSLog(#"Checkpoint !");
[self presentModalViewController: cameraUI animated: YES];
}
} }
-(void)viewDidLoad
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
[self.view addSubview:imagePicker.view];
}
else
{
// UIAlertView…
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:NO completion:NO];
}
I want to put out an alert when you do not have a camera.
IPhone app launch and move in this code.
But, Crash (This Error >
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CameraAppDelegate class])); > Thread 1: signal SIGABRT) when run in a simulator.
Why is this?
use this code and add UIImagePickerControllerDelegate delegate in .h file
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.wantsFullScreenLayout = YES;
[self presentModalViewController:picker animated:YES];
}
else
{
UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:#"Camera Not Available" message:#"Camera Not Available" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
altnot.tag=103;
[altnot show];
[altnot release];
}
Create NSObject Class and name it like ClsGlobal, or the name you want..
then write +(BOOL)isCameraDeviceAvailable in your ClsGlobal.h and implement below function in ClsGlobal.m.
+(BOOL)isCameraDeviceAvailable
{
BOOL isCameraAvailable=NO;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
isCameraAvailable = YES;
}
return isCameraAvailable;
}
Use this class method, It will return YES if camera available else return NO.
Now you can call this method using [ClsGlobal isCameraDeviceAvailable]; means your if Condition looks like if([ClsGlobal isCameraDeviceAvailable]).
This method will help you throughout your project in any controller, You have to just import ClsGlobal like #import "ClsGlobal.h".
I have a universal app in Xcode. If the user is using an iPad the use image from library button works great. However if they use an iPhone the button doesn't work.
Here is the error I receive.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'
Was told this code would work. Does it go into the (IBAction) Code below?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { Add Popover code here } else {
Add alternative for popover here }
- (IBAction) useCameraRoll: (id)sender
{
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
} else {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
newMedia = NO;
}
}
}
This crash is occuring because you can't use UIPopOverControllers on iPhones, only on iPads.
[UIPopoverController initWithContentViewController:] called when not
running under UIUserInterfaceIdiom Pad.'
Try something like this:
- (IBAction)useCameraRoll:(id)sender
{
UIPopoverController *popoverController = [[UIPopoverController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
newMedia = NO;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (popoverController.isPopoverVisible) {
[popoverController dismissPopoverAnimated:YES];
}else{
[popoverController setContentViewController:imagePicker];
[popoverController setDelegate:self];
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 320, 320) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}else{
[imagePicker presentViewController:imagePicker animated:YES completion:nil];
}
}
I saw a strange behaviour today. I was trying to open uiimagepickercontroller from a viewcontroler called lets say "BloodWingViewController (subclass of viewcontroller)". Here is the code to display picker:
if (self.picker == nil) {
[SVProgressHUD showWithStatus:#"Loading picker..."];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
self.picker = [[[UIImagePickerController alloc] init] autorelease];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.allowsEditing = NO;
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController presentModalViewController:picker animated:YES];
[SVProgressHUD dismiss];
});
});
} else {
[self.navigationController presentModalViewController:picker animated:YES];
}
Well above code works absolutely fine, no problem with that. The strange thing i saw was if i get into this so called "BllodWingViewController" through push seague and try to open picker then it works. But if i get into again so called "BllodWingViewController" through modal seague and try to open picker then it doesnt work at all. No picker opens up. Can u explain me the reason behind this ? And How to overcome this issue since i have to open picker from viewcontroller that has been exec via modal seague connection.
Sorry for my english if it confuses you :P
EDIT:
- (IBAction)onPhotoPickerClick:(id)sender {
if (self.picker == nil) {
[SVProgressHUD showWithStatus:#"Loading picker..."];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
self.picker = [[[UIImagePickerController alloc] init] autorelease];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.allowsEditing = NO;
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController presentModalViewController:picker animated:YES];
[SVProgressHUD dismiss];
});
});
} else {
[self.navigationController presentModalViewController:picker animated:YES];
}
}
#import <UIKit/UIKit.h>
#interface EditStudentViewController : UIViewController{
UIImagePickerController * picker;
}
(IBAction)onPhotoPickerClick:(id)sender;
#property (retain, nonatomic) UIImagePickerController * picker;
#end
Now when i am coming to EditStudentViewController using push segue connection, onPhotoPickerClick is working, means photolibary shows up and i can select images. But when i come to EditStudentViewController from previous viewcontroller then photolibary doesnt shows up at all.
Thanks.
I have implemented picture taking while pressing UI Button butwhen ever i pressed the button got app crashed.
Here is the source code.
.h file
#interface Camera : UIViewController
<UIImagePickerControllerDelegate>
{
UIImagePickerController *mPicture;
}
#property (nonatomic, retain) UIImagePickerController *mPicture;
.m file
#implementaion Camera
#synthesize mPicture;
-(void)pictureButtonPushed
{
UIImagePickerControllerSourceType mType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:mType])
{
mPicture.sourceType = mType;
[self presentModalViewController:mPicture animated:YES];
}
}
Thanks in advance
Try this one
Hope it will help :)
-(IBAction)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
-(IBAction)chooseFromLibrary
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImageView setImage:image]; // "myImageView" name of any UImageView.
}
Here is the code for what you want
- (void)cameraPressed
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle:#"Set a Prifile Picture"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose From Library",#"Take a New Photo",nil];
[menu showInView:[self.navigationController view] ];
}
// actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
#try {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
else {
[appDelegate showAlertWithTitle:#"Info" message:#"This function needs a camera which is only available on the iPhone or iPod."];
}
}
#catch (NSException *e) {
}
}
if (buttonIndex == 0) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//flag = TRUE;
[self dismissModalViewControllerAnimated:YES];
//[appDelegate showLoadingView];
UIImage *capturedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
Use this image object where ever you want to use.