#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]);
}
}];
}
网友评论