Ios 使用MPMoviePlayerController时收到内存警告

Ios 使用MPMoviePlayerController时收到内存警告,ios,memory-management,ios7,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

当从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];
                      });
  }}
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0),^(void){
对于(int i=0;i60:%@),NSStringFromCGSize(newImage.size));
UIGraphicsSendImageContext();
//检查是否正确提取了缩略图
如果(新图像)
[durationArray addObject:newImage];
其他的
NSLog(@“零缩略图”);
dispatch\u async(dispatch\u get\u main\u queue()^{
[自行设计ThumbScroll:thumbImageCount];
});
}}

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

一般来说,在内存中加载视频是昂贵的

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

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

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

尝试通过

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

请把警告放在这里.“收到内存警告”谢谢你的回复。它适用于小回路,例如:1到60,长回路不工作。也有同样的警告。如果是这样的话,也许可以把它分成一系列更小的循环?