简体   繁体   English

内存泄漏使用MPMoviePlayerController

[英]Memory Leak in using MPMoviePlayerController

In my project I manually create views without using storyboard. 在我的项目中,我手动创建视图而不使用故事板。 I have tried to play a Video when I tap an image. 我点按图片时尝试播放视频。 It works fine. 它工作正常。 But everytime when I check it shows memory leak when I tap the image, I have searched about this a lot and applied but no use. 但每当我检查它显示内存泄漏时,我点击图像,我已经搜索了很多,并应用但没有用。 In my Appdelegate.h file: 在我的Appdelegate.h文件中:

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

In .m file: 在.m文件中:

-(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(), 在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 everytime it enters imageTapped: method. 每次进入imageTapped:方法时我都会收到内存泄漏。 Any help will be appreciated. 任何帮助将不胜感激。

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

another think is that your theMoviePlayer is not remove, try to make its view transparency for see if is already working behind the new one 另一个想法是你的theMoviePlayer没有删除,试图让它的视图透明,看看是否已经在新的后面工作

I am trying to help you. 我想帮你。

In -(void) startPage you allocate _image1 object 在 - (void)startPage中分配_image1对象

And you are removing object in method -(void) image1Tapped:(UITapGestureRecognizer*)sender using [_image1 removeFromSuperview]; 你正在使用[_image1 removeFromSuperview]删除方法- (void)image1Tapped:(UITapGestureRecognizer *)发送者中的对象; method 方法

means now _image1 is nil and when -(void) image1Tapped:(UITapGestureRecognizer *)sender method called at that time when u fetch _image1 object at that time _image1 is already nil so its give Memory Leak warning. 意味着现在_image1是nil并且当- (void)image1Tapped :( UITapGestureRecognizer *)发送方法在此时调用当你获取_image1对象时_image1已经是nil 所以它给出了Memory Leak警告。

And solution for this one is: 这个解决方案是:

1. **Show/Hide _image1 object** or every time you need to do proper allocation and remove image1 object in this method -(void) image1Tapped:(UITapGestureRecognizer *)sender , based your requirement. 1. **显示/隐藏_image1对象**或每次需要进行正确分配并删除此方法中的image1对象- (void)image1Tapped:(UITapGestureRecognizer *)sender ,根据您的要求。

First try this solution and there will be Memory Leak Warning removed. 首先尝试此解决方案,将删除内存泄漏警告。

The Compiler check all over steps in advance so it recognize as Warning. 编译器提前检查所有步骤,以便识别为警告。

In Some cases if your logic is wrong then compiler inform us Wrong logic. 在某些情况下,如果您的逻辑错误,那么编译器会告诉我们错误的逻辑。

if u want to check that click on blue arrow of memory warning button it will give exaplanation of your logic or warning with assumption. 如果你想检查点击内存警告按钮的蓝色箭头,它会假设出现你的逻辑或警告。

I found the problem, its with the device iPad(iOS version 5). 我发现了问题,它与设备iPad(iOS版本5)。 It shows no leaks when I checked with iPad4 (iOS version 7). 当我用iPad4(iOS版本7)检查时,它没有显示泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM