美文网首页
iOS 获取网络视频第一帧

iOS 获取网络视频第一帧

作者: 赵哥窟 | 来源:发表于2020-12-24 13:40 被阅读0次

    现在的后台越来越懒了,本来列表里面的图片属性应该返回一个图片的URL地址。
    但是奇葩的后台会返回两种,一种是图片链接,一种是视频链接、如果是视频的
    话就需求自己取视频的第一帧,而且还没有字段判断该链接是图片链接还是视频链接,这也难道不到咱。实现方法如下。

    #import "VideoHelper.h"
    #import <AVFoundation/AVAsset.h>
    #import <AVFoundation/AVAssetImageGenerator.h>
    #import <AVFoundation/AVTime.h>
    #import <SDWebImage/SDImageCache.h>
    
    
    @implementation VideoHelper
    
    +(void)getVideoPreViewImageURL:(NSString *)videoURL forImageView:(BaseImageView *)imageView placeHolderImage:(UIImage *)placeHolder
    {
    
        [[SDImageCache sharedImageCache] queryCacheOperationForKey:videoURL done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
             //是否有缓存图片
            if(image){
                imageView.image = image;
            }else{
                //获取视频第一帧
                [self getVideoFirstViewImage:videoURL forImageView:imageView placeHolderImage:placeHolder];
            }
        }];
    }
    
    // 获取视频第一帧
    + (void)getVideoFirstViewImage:(NSString *)videoURL forImageView:(BaseImageView *)imageView placeHolderImage:(UIImage *)placeHolder {
       
        NSString *url = [NSString stringWithFormat:@"%@?flag=2",videoURL];
        __block UIImage *videoImage;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:url] options:nil];
            NSParameterAssert(asset);
            AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
            assetImageGenerator.appliesPreferredTrackTransform = YES;
            assetImageGenerator.apertureMode =AVAssetImageGeneratorApertureModeEncodedPixels;
            CGImageRef thumbnailImageRef = NULL;
            NSError *thumbnailImageGenerationError = nil;
            thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(0, 60)actualTime:NULL error:&thumbnailImageGenerationError];
            if(!thumbnailImageRef)
            NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
            videoImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef]: nil;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //主线程更新UI
                if(videoImage){
                    imageView.image = videoImage;
                    //缓存图片
                    [[SDImageCache sharedImageCache] storeImage:videoImage forKey:videoURL toDisk:NO completion:^{
                        
                    }];
                    
                }else{
                    //如果不是视频就设置图片
                    [imageView setImageUrl:videoURL placeholderImage:placeHolder];
                }
            });
              
        });
    
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS 获取网络视频第一帧

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