UIImagePickerController when didFinishPickingMediaWithInfo is not implemented

A quick conceptual question,

If I use UIImagePickerController and I do not implement didFinishPickingMediaWithInfo or try to process the returned UIImage after the image is “taken”, what happens to the UIImage data that would be returned during the delegate call, is this just released by the system?

After testing, it does not seem to flow or add to the standard photo library.

Thanks,

+4
source share
1 answer

I do not understand why this would be in the first place.

You should think that the code that Apple developers already wrote will surely release the image that the camera took after completing the delegate call. For example, suppose this is what a developer might look like (pre-ARC to show you that you don't even need ARC for this).

- (IBAction)userDidPressAccept:(id)sender { // Obtain image from wherever it came from, this image will start with // Retain Count: 1 UIImage *image = [[UIImage alloc] init]; // Build an NSDictionary, and add the image in // Image Retain Count: 2 NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:image, UIImagePickerControllerOriginalImage, ..., nil]; // Now the dictionary has ownership of the image, we can safely release it // Image Retain Count: 1 [image release]; if ([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)]) { // Guy sees what he does with his image // Image Retain Count: X (Depends on the user) [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:image]; } // Ok, we're done. Dictionary gets released, and it can no longer own the image // Image Retain Count: X-1 (Depends on the user) [userInfo release]; } 

In the example, if the user did not retain image (or even did not implement the method), then X will be 1, and when he reaches the final release , the image will disappear well. If the user saves the image, the image will work, but a dictionary that supports it can get dealloc -ated.

This is the basic concept of “ownership” that comes with reference counting, like a glass ball that needs to be passed by hand, and if the ball does not have an arm under it, it will fall and break.

ARC seems to mask all this, doing it yourself, but the basic concepts remain, ownership is transferred to the implementation of the delegate, and if there is no delegate who claims it, it will be deleted.

+1
source

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


All Articles