iOS 7 crashes BAD_ACCESS when using UIImagePickerController - ios

I am trying to use a UIImagePickerController to let my users select photos or take photos to be used in my app. But the app seems to crash most of the time whenever a image is selected from their photos or when they choose to use a photo taken. I used NSZombie objects and I get this message:
[NSISRestrictedToNonNegativeVariable retain]: message sent to deallocated instance 0x168c3530
I don't know what is going on here. This same problem occurred when I was presenting a modal view occasionally.
Here is my code for presenting the UIImagePickerController and handling the response:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:CANCEL_TITLE]) {
return;
}
UIImagePickerControllerSourceType sourceType = buttonIndex == 0 ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
UIImagePickerController* controller = [[UIImagePickerController alloc] init];
controller.sourceType = sourceType;
[controller setDelegate:self];
if (buttonIndex == 1) {
//PNCoverOverlayView* overlayView = [PNCoverOverlayView viewWithNibNamed:nil];
//[controller setCameraOverlayView:overlayView];
[controller setShowsCameraControls:YES];
}
[self presentViewController:controller animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage* originalImage = info[UIImagePickerControllerOriginalImage];
_imageToUpload = originalImage;
CGFloat scaleAdjust = ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2) ? 0.5 : 1;
CGFloat length = LENGTH * scaleAdjust;
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
PNCoverOverlayView* overlay = (PNCoverOverlayView*)[picker cameraOverlayView];
CGSize overlaySize = [overlay frame].size;
UIImage* cropped = [UIImage imageCroppedToSize:overlaySize fromImage:originalImage];
_imageToUpload =[UIImage imageResizedToSize:CGSizeMake(length, length) fromImage:cropped];
} else {
_imageToUpload = [UIImage imageScaledToMaximumLength:length fromImage:originalImage];
}
[self performSegueWithIdentifier:#"reviewPhotoUpload" sender:self];
}];
}
I commented out adding a custom overlay to the UIImagePickerController, because that fixed the app crashing when presenting the UIImagePickerController

You should check for the following things:
Are you testing it on device ?
Follow the steps mentioned under Overview in this apple guideline, and take care of the device on which you are running. e.g.,
iPhone or iPad.
Check for isSourceTypeAvailable also.
Also, Check for isCameraDeviceAvailable property.
When trying to get cameraOverlayView, you should check if its not nil. See the documentation of this property for more
information.
Hope it helps!

You are instantiating the controller in your function:
UIImagePickerController* controller = [[UIImagePickerController alloc] init];
You should keep a strong reference to the controller in your class, an ivar would be a good choice for that.

Related

using image picker controller the app is randomly crashed in iOS 10

I am creating an iPad app which have more than 100 question. User get answare each of the questions and also take a picture for every questions. When i run thease app in ios 9 it's work fine but when the project run into iOS 10, the app is crashed(when user take more than 30 images). i couldn't find any solutions.
here is my sample code....
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInf{
isImageChanged = YES;
[self.btnSelectImage setImage:image forState:UIControlStateNormal];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
//Edited by Sarfaraj on 10th Sept, 2014 == >
[popOverSelectImage2 dismissPopoverAnimated:YES];
[self.captionView removeFromSuperview];
UIViewController *vc = [[UIViewController alloc]init];
// vc.view = self.captionView;
vc.view.frame = self.captionView.frame;
[vc.view addSubview: self.captionView];
/*
[vc.view setFrame:self.captionView.frame];
for (UIView *object in self.captionView.subviews) {
[vc.view addSubview:object];
}
*/
// < ==
[popOverSelectImage dismissPopoverAnimated:NO];
popOverSelectImage = [[UIPopoverController alloc] initWithContentViewController:vc];
[popOverSelectImage setPopoverContentSize:CGSizeMake(self.captionView.frame.size.width, self.captionView.frame.size.height) animated:NO];
//popOverSelectImage.contentViewController = vc;
[popOverSelectImage presentPopoverFromRect:currBtnRectForPopoverImage inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:NO];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
imageToSave = image;
}
Also i convert picking images. Can any one assist me?It's great helpfull for me. And one more thing, when i run the project into simulator, it doesn't crashed.And console part i am getting --> Communications error: { count = 1, contents =
"XPCErrorDescription" => { length = 22, contents = "Connection interrupted" }
I think using dispatch may solve this problem, try using
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// your code
});
}

capture image is not working properly.?

Hi I am very new to developing iOS application please help me. i am facing a problem regarding capturing image by using UIImagePicker. I have integrated hip mob app in to our application.
In that window i want to show a UIButton so that the user click on that UIButton should capture the image. when i click on the button its throwing an message as (whose view is not in the window hierarchy!).
I think this happens due to this chat window. here is my code snippet.!
[[HMService sharedService] openChat:self withSetup:^(HMChatViewController * controller)
{
controller.chatDelegate = self;
controller.chatView.receivedMessageFont = [UIFont systemFontOfSize:20.0];
controller.navigationBarHidden = YES;
UIView * viewObjForVehicleDetails = [[UIView alloc]init];
UIButton * btnForCaputrePhoto = [[UIButton alloc]init];
[btnForCaputrePhoto addTarget:self action:#selector(CapturePhotoImage) forControlEvents:UIControlEventTouchUpInside];
btnForCaputrePhoto.frame = CGRectMake(55, 90, 103, 85);
[controller.chatView addSubview:viewObjForVehicleDetails];
[viewObjForVehicleDetails addSubview:btnForCaputrePhoto];
[[controller.chatView table] registerClass:[ChatTableViewCell class] forCellReuseIdentifier:#"ReuseChatTableViewCell"];
}
-(void)CapturePhotoImage
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:NO completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Thanks in Advance.!
First be sure you are using the following delegate in your view controller
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
Secondly use this line to show the ImagePicker:-
[self presentModalViewController:picker animated:YES];
Alternative Way:-
[self.view addSubview:imagePicker.cameraOverlayView]
Change the method declaration as follows
- (IBAction) CapturePhotoImage:(id)sender
{
NSLog("Called");
}
And the method call "CapturePhotoImage" as CapturePhotoImage:
[btnForCaputrePhoto addTarget:self action:#selector(CapturePhotoImage:) forControlEvents:UIControlEventTouchUpInside];

Disable camera rotation in iOS?

My code to add a UIImagePickerController:
picker = [[[UIImagePickerController alloc] init] autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
[self displayContentController:picker inView:cameraBoxView];
[self.view sendSubviewToBack:picker.view];
- (void)displayContentController:(UIViewController *)content inView:(UIView *)v {
//Delete the previous instance of the same view controller
for (UIViewController *vc in self.childViewControllers) {
if ([vc isKindOfClass:[content class]]) {
[vc removeFromParentViewController];
}
}
[self addChildViewController:content];
content.view.frame = v.frame;
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
if ([content isKindOfClass:[CCInnerNavigationController class]]) {
innerNavigationController = (CCInnerNavigationController *)content;
}
}
I have disabled all the device orientations except of portrait. But the image from camera is rotated. How to solve this issue in both iOS 6 and 7.
P.S. I understand that there are a lot of soludions but it seems they are too old because NOTHING of them works.
When you instatiate the UIImagePickerController you must also pass it a delegate. The delegate should be conform to 2 protocols UIImagePickerControllerDelegate and UINavigationControllerDelegate. The second makes possible to add some logic for rotation at runtime, implement this methods accordingly to what you want to achieve.
– navigationControllerPreferredInterfaceOrientationForPresentation:
– navigationControllerSupportedInterfaceOrientations:
I changed the following code:
imgView.image = img;
to:
imgView.image = [UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationRight];
And now it works in both iOS 6 and 7. I don't know how it fixes this bug but it really works. Maybe image picker corrupts the image or image view and this code fixes it by creating a new image instance.

Sorting a Picture to a Specific Place in the app

I want it to flow like this I'm doing it tons of times and dosent work.
User pushes the photo button in the app and takes picture.
After taking the picture users has to input detail. sorts options comes out where user can pick 8 different default genre's, and user pushes save.
It goes back to the Home Screen and the User can see the 8 different genre in button & when pushed pictures comes out as a coverflow(flow cover) that is saved in the app. I want to make it work like the above but dosent work.
My Code is until now is:
#implementation ViewController
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType: UIImagePickerControllerSourceTypeCamera ];
[self presentViewController:picker animated:YES completion:NULL];
}
-(IBAction)ChooseExisting{
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *) info {
image = [info objectForKey:UIImagePickerControllerOriginalImage] ;
[imageview setImage:image];
[self dismissViewControllerAnimated:YES completion:NO];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
#implementation CVCLCoverFlowLayout
-(NSInteger)count {
return [self.collectionView numberOfItemsInSection:0 ];
}
-(CGSize)collectionViewContentSize{
CGSize size = self.collectionView.bounds.size;
size.width = self.count * self.cellInterval;
return size;
}
You are not saving the image you clicked in the delegate function .and when you want to use the saved image then you need to write down the code for same .:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *) info {
image = [info objectForKey:UIImagePickerControllerOriginalImage] ;
[imageview setImage:image];
/ save the image to local storage of your application /
[self dismissViewControllerAnimated:YES completion:NO];
}
You can check the code for same over :
http://dcraziee.wordpress.com/2013/05/20/how-to-save-and-get-image-from-cache-directory/
Please share the blog if you like it.

iOS5 camera roll and camera issues when selecting/using image (fine in iOS 4.x.x and 3)

Two issues are showing up now that I want to update my app that previously worked fine in iOS4.x.x and below. Previous version of app running on iOS5.0.1 works though and I did not update any of the code around the camera functions
Camera - after user takes a picture, you get the standard "Retake" or "Use" buttons. Retake is fine but "Use" causes the app to freeze and crash.
Camera Roll - after user chooses image from camera roll, nothing appears to happen. It actually did select though -- but now you have to click "Cancel" and the image appears in the main window.
In both cases, no error messages in the debugging window so not sure what happened. Code for the two functions in question:
//Camera Method//////////////////////////
- (IBAction) cameraDeviceAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.delegate = self;
imgPicker.allowsImageEditing = NO;
if([ApplicationUtils getSystemVersionAsAnInteger] >= __IPHONE_3_1) {
if(cameraOverlayView == nil) {
cameraOverlayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo_guideline_640x960.png"]];
cameraOverlayView.frame = CGRectMake(0, 0, 320, 431);
}
imgPicker.cameraOverlayView = cameraOverlayView;
}
[self presentModalViewController:imgPicker animated:YES];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self showBackdropScreen:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
}
//Photo Library Method //////////////////////////
- (IBAction) libraryAction {
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsImageEditing = NO;
imgPicker.delegate = self;
[self presentModalViewController:imgPicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
[self showBackdropScreen : img];
}
I believe that the error lies somewhere in there, but please let me know what you think or if you need more of the file.
Thanks!
Although it a old question but the answer is :
use [self dismissModalViewControllerAnimated:YES]; in function - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;in the end

Resources