使用MPMoviePlayerController时收到内存警告

时间:2014-01-07 06:37:01

标签: ios memory-management ios7

获取"收到内存警告"当拿着缩略图形式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];
                      });
  }}

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

2 个答案:

答案 0 :(得分:0)

通常,在内存中加载视频的费用很高。

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

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

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

答案 1 :(得分:0)

尝试通过

获取缩略图
    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;
}