UIImagePickerController Leaks for Iphone Only - ios

I am getting a leak with the UIImagePickerController when exiting from the picture selection back to the application. This does not happen every time but it does happen often. Of interest is that it never happens when running on the iPad, just the iPhone and both are using the same code. I am testing on actual devices and not the simulator and I am using ARC.
The leaked object is "UIStatusBarHideAnimationParameters". The leak is always 48 Bytes. I read elsewhere to make sure to dismiss the delegate which I am doing via:
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
picker.delegate = nil;
}
The controller is being instantiated with the following:
-(void)imageFromCamera:(BOOL)camera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
if (camera) imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
else imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
Here is the delegate that is called when a picture is selected. Of note, a picture does not need to be selected to cause this issue. I can simply "cancel" and get this leak.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image;
if (info[UIImagePickerControllerEditedImage])
{
image = info[UIImagePickerControllerEditedImage];
}
else
{
image = info[UIImagePickerControllerOriginalImage];
}
self.fullUploadImage = image;
self.thumbNailImage = [self resizeImage:image];
imageSelected = YES;
[self.sendButton setEnabled:YES];
[picker dismissViewControllerAnimated:YES completion:nil];
picker.delegate = nil;
}

You can resolve this using imagePicker like an iVar allocanting when and if you need:
in .h
#property (strong, nonatomic) UIImagePickerController *imagePicker;
on .m
- (UIImagePickerController *) imagePicker {
if(!_imagePicker){
_imagePicker = ((UIImagePickerController alloc) init)
}
return _imagePicker
}
-(void)imageFromCamera:(BOOL)camera
{
if (camera) self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
else self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}

Related

I can't set background image inside UIButton for large image from iPad in xcode 7.2

I want to set background image inside UIButton in objective C using xcode 7.2.
It is working very well on iPhone and iPod, but on iPad it is not working as my requirement.
I think that the reason is image is too large in iPad.
Here is my code.
-(void) showImagePickerController {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingImage:(UIImage )image editingInfo:(NSDictionary<NSString ,id> )editingInfo {
tempImage = image;
[self showResult];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void) showResult {
[button setBackgroundImage:tempImage forState:UIControlStateNormal];
}
Thank you.

UIImagePickerController causing crash

I'm using ios 8.4. When debugging an app, and I present a UIImagePickerController, xcode loses connection with the iphone. This wasn't a problem before.
Sometimes it will bring up the image picker... but then when I save an image, there'll be a crash.
Is anyone else experiencing this? How to fix?
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.editing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls=YES;
[self presentViewController:imagePickerController animated:YES completion:nil];
try this code
- (IBAction)galleryButtonPressed:(id)sender
{
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.allowsEditing = YES;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:NULL];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil)
image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Do something with the image
[self.imageView setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:NO completion:nil];
}

UIImagePickerController won't cancel, getting message about detached ViewControllers

I've not seen these two issues compounded together, so I figured I'd ask.
PhotoViewController
-(void)viewDidAppear:(BOOL)animated
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
if(!image) image = info[UIImagePickerControllerOriginalImage];
picker.delegate = self;
[self setPhotoForNextVC:image];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"givePhotoToGiveDetailsSegue" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
Selecting a photo works, but when I press cancel the UIImagePickerController keeps coming back up, and I get the error:
Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x10b38b560>.
I had faced this problem before as well.
For me it turned out to be that, especially in ViewDidAppear.
The OS doesn't like to put up new controllers right after another appears.
Make another method that handles this.
(void)bringUpCameraController {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
My workaround :[self performSelector:<(bringUpCameraController)> withObject:<(nil)> afterDelay:<(2.0)>]
this will get rid of the message.
Look up the tree to PhotoViewController's hierarchy. It's probably not the root. I had this happen.
The error message is literal.

ioS7: Capture multiple images from camera and store in array

guys.
I'm trying to capture multiple images from the camera in iOS7, when I capture the first image and click in in "Use Photo", then it's store the image in a array in didFinishPickingMediaWithInfo. When the camera appear again, I see the last image in background but I can't see the camera movement. I know the camera is enabled, because it's searching faces. The code is below:
- (IBAction)openTour:(id)sender
{
_counter=0;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = YES;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
imagePicker = nil;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
UIImage *image= [info objectForKey:UIImagePickerControllerEditedImage];
[ _imageArray addObject:image ];
_counter++;
if ( _counter<5 )
{
[self dismissViewControllerAnimated:NO completion:nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = YES;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:NO completion:nil];
imagePicker = nil;
}
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Does anyone know what is happening?
Thanks in advance!
Set showsCameraControls = NO;
Use cameraOverlayView property.
In the action of your camera button call takePicture method.
Keep adding UIImage objects received by the method
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

iOS: Screen no longer receives Touches after calling dismissModalViewController

I have two views. Very simple set up just to play around with the camera roll.
One view will bring up the camera roll using:
[self presentViewController:self.imagePicker animated:NO completion:nil];
which is triggered by a UIButton. It brings up the camera, everything's fine.
After taking a picture, I am calling the following to dismiss the controller:
[self dismissViewControllerAnimated:NO completion:nil];
Again, everything is working.
But the view that brings up the imagePicker Controller no longer receives touch (the UIButton will no longer brings up the camera again) after the Modal View is dismissed.
It will only start to receive touches again when I switch to another view and come back.
I have been searching for a solution to this problem but have not been successful in finding anything. Thanks in advance.
EDIT (Adding code):
In CameraController.m
This is where brings up the Camera Roll
(Subclass of UIViewController conforming to
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
//UIButton that brings up the imagePicker
- (IBAction)useCamera
{
[self prepareImagePicker];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
//Initializing ImagePicker
- (void)prepareImagePicker
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
self.imagePicker.delegate = self;
OverlayCameraView *cameraOverlay = [[OverlayCameraView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
self.imagePicker.cameraOverlayView = cameraOverlay;
}
}
//Delegate Method when a picture is taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imagePicker.delegate = nil;
[picker dismissViewControllerAnimated:NO completion:nil];
self.imagePicker = nil;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
In OverlayCameraView.m
//Camera Overlay Class (Subclass of UIView)
- (id)initWithFrame:(CGRect)frame andTheCameraController:(CameraController*)cameraController
{
if ((self = [super initWithFrame:frame]))
{
self.camera = cameraController;
//.…2 UIButtons set up
}
return self;
}
//Overlay Cancel Button
- (void) cancel
{
[self.camera dismissViewControllerAnimated:NO completion:nil];
}
//Overlay Take Picture Button
- (void) takeAPicture
{
[self.camera.imagePicker takePicture];
}
You should be calling dismissViewControllerAnimated on the picker not self via the UIImagePickerControllerDelegate methods.
[picker dismissViewControllerAnimated:NO completion:nil];
or if you are keeping a reference to it:
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
EDIT:
Based on the revised code, I believe
self.imagePicker.cameraOverlayView = cameraOverlay;
should be:
self.imagePicker.cameraOverlayView = cameraOverlay.view;
Also this will cause a +2 reference count and will leak if you are not using ARC:
self.imagePicker = [[UIImagePickerController alloc] init];
It should be for proper reference counting:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.imagePicker = picker;
[picker release];

Resources