【问题标题】:Received memory warning when use MPMoviePlayerController使用 MPMoviePlayerController 时收到内存警告
【发布时间】:2014-01-07 06:37:01
【问题描述】:

当从 MPMoviePlayerController 中获取缩略图并且应用程序崩溃时收到“收到内存警告”。

我正在使用代码:

   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

    for (int i = 0; i < pickedVideoAssetDuration; i ++){

        UIImage *singleFrameImage = [movie thumbnailImageAtTime:i timeOption:MPMovieTimeOptionExact];
        CGSize newSize = CGSizeMake(50, 50);
        // checks whether the thumbnails are properly extracted or not
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0f);
        [singleFrameImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        NSLog(@"newSize >60 : %@", NSStringFromCGSize(newImage.size));
        UIGraphicsEndImageContext();

        // checks whether the thumbnails are properly extracted or not
        if(newImage)
            [durationArray addObject:newImage];
        else
            NSLog(@"nil thumbnail");

        dispatch_async(dispatch_get_main_queue(), ^{
                          [self  designthumbScroll:thumbImageCount];
                      });
  }}

如果有任何帮助将不胜感激:)

【问题讨论】:

  • 请把警告放在这里..
  • “收到内存警告”

标签: ios memory-management ios7


【解决方案1】:

一般来说,在内存中加载视频的成本很高。

也许尝试在循环内部添加一个@autoreleasepool 以稍微更快地释放内存:

for (int i = 0; i < pickedVideoAssetDuration; i ++)
{
    @autoreleasepool{
        // your existing code
    }
}

否则,请查看您的其余代码,看看您当时可以释放多少内存。

【讨论】:

  • 感谢您的回复。它适用于小循环,例如:1 到 60,长循环不起作用。有同样的警告。
  • 如果是这样的话,也许把它分解成一系列更小的循环?
【解决方案2】:

尝试获取缩略图

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"YourVideoFileName"];

    UIImage * thumbnailImg = [self getThumbNail:newPath];

以及方法代码

-(UIImage *)getThumbNail:(NSString *)stringPath
{
    NSURL *videoURL = [NSURL fileURLWithPath:stringPath];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

    [player stop];
    return thumbnail;
}

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    相关资源
    最近更新 更多