Always get memory warning when using UIImagePicker for camera

I am using UIImagePicker in viewController ,

and there are two types of methods in which I always receive a warning about memory, as well as the very well-known "wait_fences: did not receive an answer: 10004003"

but I cannot trace to a specific line of code that asks for a warning - it always comes right after these methods, where I cannot debug.

 // in myViewController.h // the first 2 are the methods that I alloc my UIImagePicker, // here, self.photoPicker is a retained property of UIImagePicker. - (IBAction)fromAlbumButtonTapped { if (self.photoPicker == nil) { self.photoPicker = [[[UIImagePickerController alloc] init] autorelease]; self.photoPicker.delegate = self; } if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { self.photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:self.photoPicker animated:YES]; return; } } - (IBAction)fromCameraButtonTapped { if (self.photoPicker == nil) { self.photoPicker = [[[UIImagePickerController alloc] init] autorelease]; self.photoPicker.delegate = self; } if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentModalViewController:self.photoPicker animated:YES]; return; } } // and this is another part that gives me the memory warning - getting a photo. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { self._photo = [info objectForKey:UIImagePickerControllerOriginalImage]; self.photoView.photoView.image = self._photo; [self.photoButton setImage:self._photo forState:UIControlStateNormal]; [self dismissModalViewControllerAnimated: YES]; } 

I already checked my code and did not detect a potential memory leak, as far as I can tell.

I know that dealing with photography takes a bit of memory, so it’s normal to get a warning about the memory.

But sometimes there is a problem: viewController just release something important when a warning appears, for example, some button to return to the parentView controller in the navigation stack.

Therefore, I do not want to receive a warning that my buttons or something else important will be released too soon.

Is there any way to fix this?

+4
source share
3 answers

Not all memory "loss" is caused by a leak. Use a Heapshot.

Use tools to check for leaks and memory loss due to saved but not leaked memory. The latter is unused memory that is still pointed to. Use the Heapshot in the Allocations tool on the tools.

To use Heapshot to search for memory, see bbum's blog.

Basically, there is a method for launching tools to select tools, execute heaps, run the intuition of your code, and repeat once every 3 or 4 times. This will indicate a memory that is allocated and not issued during iterations.

To find out what results are revealed to see individual distributions.

If you need to see where they are stored, released, and autorealized for tools using the object:

Starting in the tools, in Allocations set "Record of samples" is enabled (you must stop recording to set the parameter). Try to start the collector, stop recording, find ivar (datePickerView) there, expand, and you can see where everything is saved, releases and auto-implementers happened.

+2
source

When you use the UIImagePickerController, you get a photo. The photo can be quite large. For example, with an 8MP built-in camera, this is actually 8MP * 3 bytes, since each pixel is RGB, and if it is not packed, it is 8MP * 4 = 32MB. So it is not surprising that you receive a warning about memory.

Override didReceiveMemory Continue and clear memory. In addition, views can be unloaded (prior to iOS 6), so you should be able to properly reload them in viewWillLoad. See Memory Management

0
source

Change the following

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [imageView setImage:image]; [self dismissModalViewControllerAnimated:YES]; } 

To the next...

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self dismissModalViewControllerAnimated:YES]; [imageView setImage:image]; } 
0
source

Source: https://habr.com/ru/post/1399009/


All Articles