简体   繁体   English

使用MPMoviePlayerController时收到内存警告

[英]Received memory warning when use MPMoviePlayerController

Getting "Received memory warning" when take the thumbnails form MPMoviePlayerController and application is crashed. 以缩略图形式从MPMoviePlayerController获取“接收到的内存警告”并且应用程序崩溃了。

I am using the code : 我正在使用代码:

   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];
                      });
  }}

Will appreciated if any help :) 如果有任何帮助将不胜感激:)

Videos are expensive to load in memory in general. 通常,将视频加载到内存中非常昂贵。

Maybe try adding an @autoreleasepool to the inside of your loop to release memory slightly faster: 也许尝试在循环内部添加@autoreleasepool以稍微更快地释放内存:

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

Otherwise, look at the rest of your code and see what memory you can release at the time. 否则,请查看其余代码,并查看当时可以释放的内存。

Try to get thumbnails by 尝试通过获取缩略图

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

    UIImage * thumbnailImg = [self getThumbNail:newPath];

And method code 和方法代码

-(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;
}

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

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