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

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
});
}

Related

iOS 7 crashes BAD_ACCESS when using UIImagePickerController

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.

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.

ImagePickerController crashes my app saying "Received Memory Warning" when there are other apps running on my device (2 or more apps)

My App crashes saying "Received Memory Warning" when i take a picture from camera (UIImagePickerController).
Scenario when i get the crash: If I have other apps running on my device(2 or more), I am getting the crash and if there is only one other app running or no other apps running before I launch my app then there are no crashes at all.
This crash happens only when i take the picture from my camera but when i choose the picture from photo library there is no crash at all.
I am using Xcode 4.3.2 and I am using ARC.
Can any one help me on this?
This is the code i am using
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex: (NSInteger)buttonIndex
{
if (buttonIndex < 2)
{
if([UIImagePickerController isSourceTypeAvailable:buttonIndex==0?UIImagePickerControllerSourceTypeCamera:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
[ipc setDelegate:(id<UIImagePickerControllerDelegate,UINavigationControllerDelegate>) self];
[ipc setSourceType:buttonIndex==0?UIImagePickerControllerSourceTypeCamera:UIImagePickerControllerSourceTypePhotoLibrary];
if (buttonIndex == 0)
{
[ipc setAllowsEditing:NO];
}
[ipc setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]];
[self presentModalViewController:ipc animated:YES];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Action Alert" message:#"Camera is not available in this device." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];
[NSThread detachNewThreadSelector:#selector(scallImage:) toTarget:self withObject:image];
}
-(void) scallImage:(UIImage *) image
{
CGSize newSize = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
image=nil;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *cImage = [UIImageJPEGRepresentation(newImage, 0.5) base64EncodedString];
[ImageButton setBackgroundImage:newImage forState:UIControlStateNormal];
[ImageButton setHidden:NO];
//[profileImageButton setBackgroundImage:newImage forState:UIControlStateNormal];
newImage=nil;
}
Is there any thing wrong in my code?
Try avoiding to allocate UIImagePickerController in that method. Instead allocate it in viewDidLoad. Put this line of code in viewDidLoad:
ipc = [[UIImagePickerController alloc] init];
And have it declared first in .h file.
Hope this helps.
I finally realized that by removing some of the unwanted images/resources from the project I am able to run my app without crashes ...!!! Thanks everyone who helped me !

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

open UIImagePickerController in landscape mode

I want to open iphone saved images in my application. my application working in landscape mode. when I trying to load all saved photo from iphone library using presentModalViewController method, it will open in portrait mode. I want that in landscape mode. here is the code:
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsEditing = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
[picker setNavigationBarHidden:TRUE];
[self presentModalViewController:picker animated:NO];
NSLog(#"%#", self.view.subviews);
[picker release];
can any one help me..
Thanks in advance.
You can't. Quoth the documentation:
The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.
It's a bit more work, but you can use the AssetsLibrary framework to access the list of images and create your own image picker.
we cant get UIImagePickerController in landscape...
but we can get the images in our device into an array and display them in landscape mode and in portrait mode....itseems same like UIImagePickerController...
we should use ALAsset class and ALAssetsLibrary for this..
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != NULL)
{
[assets addObject:result];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self meth];
[activity stopAnimating];
[activity setHidden:YES];
};
assets = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) { NSLog(#"Failure");}];
in viewDidLoad
-(void)meth
{
NSLog(#"%i",[assets count]);
if(userOrientation==UIInterfaceOrientationPortrait || userOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"haii");
[scrollView removeFromSuperview];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor=[UIColor whiteColor];
NSLog(#"%i",[assets count]);
for (int i = 0; i < [assets count]; i++)
{
imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imgBtn setFrame:CGRectMake((i%4*80)+2,(i/4*80)+2,75,75)];
imgBtn.tag=i;
[imgBtn addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
ALAsset *asset=[assets objectAtIndex:i];
[imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
[scrollView addSubview:imgBtn];
}
scrollView.contentSize = CGSizeMake(320,(([assets count]/4)+1)*300 );
}
if(userOrientation==UIInterfaceOrientationLandscapeRight || userOrientation==UIInterfaceOrientationLandscapeLeft)
{
[scrollView removeFromSuperview];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 480,320)];
for (int i = 0; i < [assets count]; i++)
{
imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imgBtn setFrame:CGRectMake((i%6*80)+2,(i/6*80)+2,75,75)];
imgBtn.tag=i;
[imgBtn addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
ALAsset *asset=[assets objectAtIndex:i];
[imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
[scrollView addSubview:imgBtn];
}
scrollView.contentSize = CGSizeMake(480,(([assets count]/4)+1)*300);
}
[self.view addSubview:scrollView];
}
I have tried the code with some additions
[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker.view removeFromSuperview];
[picker removeFromParentViewController];
}
(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker.view removeFromSuperview];
[picker removeFromParentViewController];
}
I used following code:
[self.view addSubview:imgPicker.view];
[imgPicker viewWillAppear:YES];
[imgPicker viewDidAppear:YES];
instead of using presentModalViewController method,
[self presentModalViewController:picker animated:NO];
I tried this approach, while following the API docs more closely:
- (void)openEmbeddedImagePicker:(UIImagePickerController *)picker {
[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];
}
But the video recorder's Cancel and playback buttons don't respond. Also, while the containing VC (self in the above method) restricts the orientation to landscape, rotating between the two landscape modes messes up the layout of the picker's overlay bar (on iOS 6, at least).
Does anyone have success restricting the video recorder to landscape mode?
From the UIImagePickerController Class Reference.
Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

Resources