獲取網路視訊任意幀畫面

weixin_34148456發表於2018-03-09

+ (void ) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time  block:(void(^)(UIImage *))block;


+ (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time block:(void (^)(UIImage *))block{
    
    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
        NSParameterAssert(asset);
        AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
        assetImageGenerator.appliesPreferredTrackTransform = YES;
        assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
        CGImageRef thumbnailImageRef = NULL;
        CMTime ztime = CMTimeMakeWithSeconds(time, 600);
        NSError *thumbnailImageGenerationError = nil;
        thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:ztime actualTime:NULL error:&thumbnailImageGenerationError];
        if(!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
        UIImage *  thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            block(thumbnailImage);
        });
            
        
        
        
    });
    


}


}

http://blog.csdn.net/oqqquzi1234567/article/details/42921025

相關文章