Memory leak when using MPMoviePlayerController

In my project, I manually create views without using storyboards. I am trying to play a video when I click an image. It is working fine. But every time I check that it shows a memory leak, when I click on the image, I searched and applied a lot, but did not use it. In my Appdelegate.h file:

@property (strong, nonatomic) MPMoviePlayerController *theMoviePlayer;
@property (strong, nonatomic) UIImageView *image1;

In the .m file:

-(void) startPage{
.....
_image1 = [[UIImageView alloc] initWithFrame:CGRectMake((self.window.frame.size.width/2)-25, 40, 50, 50)];
[_image1 setUserInteractionEnabled:YES];
_image1.image = [UIImage imageNamed:@"image_2.jpg"];
_tapImage1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(image1Tapped:)];
[_image1 addGestureRecognizer:_tapImage1];
.....}

In imageTapped (),

-(void) image1Tapped:(UITapGestureRecognizer *)sender
{
  .....

 [_image1 removeFromSuperview];

 _theMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];


    [_theMoviePlayer setControlStyle:MPMovieControlStyleFullscreen];

    [_theMoviePlayer.view setFrame:CGRectMake(0,-55, self.window.frame.size.width, self.window.frame.size.height)];



    [_theMoviePlayer setScalingMode:MPMovieScalingModeAspectFill];

    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];

    [backgroundWindow addSubview:_theMoviePlayer.view];

    [_theMoviePlayer.view bringSubviewToFront:backgroundWindow];
[_theMoviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinished:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification object:_theMoviePlayer];
...}

I get a memory leak every time he enters the imageTapped: method. Any help would be appreciated.

+4
source share
3 answers

, iPad (iOS 5). , iPad4 (iOS 7).

+1
@property (strong, nonatomic) MPMoviePlayerController *theMoviePlayer;
@property (weak, nonatomic) UIImageView *image1;

, theMoviePlayer , , .

+1

.

- (void) startPage _image1

- (void) image1Tapped: (UITapGestureRecognizer *) [_ image1 removeFromSuperview];

, _image1 , - (void) image1Tapped: (UITapGestureRecognizer *) , , u _image1 _image1 , .

:

1. ** / _image1 ** , image1 - (void) image1Tapped: (UITapGestureRecognizer *) , .

, .

, .

, , .

If you want to check that you click on the β€œBlue memory arrow” button, this will lead to exaplanation of your logic or warning with an assumption.

+1
source

All Articles