美文网首页
获取视频预览图

获取视频预览图

作者: 风动青春 | 来源:发表于2018-04-19 14:18 被阅读0次

    #pragma mark - 网络视频预览

    - (void)getEachPic {

        AVAsset *myAsset = [AVAsset assetWithURL:pathURL];

        AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];

        imageGenerator.appliesPreferredTrackTransform = YES;

        imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;

        Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);

        NSMutableArray *timesArr = [[NSMutableArray alloc] init];

        int totalSec = durationSeconds; 

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

            int sec = durationSeconds/totalSec * i;

            CMTime firstThird = CMTimeMakeWithSeconds(sec, 20);

            NSValue *value = [NSValue valueWithCMTime:firstThird];

            [timesArr addObject:value];

        }

         __block NSInteger index = 0;

        [imageGenerator generateCGImagesAsynchronouslyForTimes:timesArr completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {

            NSString *requestedTimeString = (NSString *) CFBridgingRelease(CMTimeCopyDescription(NULL, requestedTime));

            NSString *actualTimeString = (NSString *) CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));                

            NSLog(@"Requested: %@; actual %@", requestedTimeString, actualTimeString);     

            if (result == AVAssetImageGeneratorSucceeded) {

                    // Do something interesting with the image.

                    index ++;

                    UIImage *imageData = [[UIImage alloc] initWithCGImage:image];

                     dispatch_async(dispatch_get_main_queue(), ^{

                            // 获取到图片,在主线程中刷新界面

                    });

            } else if (result == AVAssetImageGeneratorCancelled) {

                    NSLog(@"Canceled");

            } else if (result == AVAssetImageGeneratorFailed) {

                    NSLog(@"Failed with error: %@", [error localizedDescription]);

            }

        }];

    }

    相关文章

      网友评论

          本文标题:获取视频预览图

          本文链接:https://www.haomeiwen.com/subject/norwkftx.html