iOS視頻封面中間原圖,背景模糊效果實(shí)現(xiàn)

一 . 視頻封面中間原圖,背景模糊效果實(shí)現(xiàn)方法( 用到的方法在文章最后)

- (void)portraitBtnClick:(id)sender {
// 0.獲取豎屏視頻
NSString *vedioPath = [[NSBundle mainBundle] pathForResource:@"portrait" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:vedioPath];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 1.獲取視頻第一幀圖片
        UIImage *cover = [self getVideoPreViewImage:url];
        
        //2.按照比例截取封面圖片
        
        //獲取coverImg(視頻封面的UI控件)的寬高比例用于截取圖片(豎屏這里截取的圖片保持寬不變,以圖片中心為基準(zhǔn),按照coverImg的比例裁剪的高,得到圖片)
        CGFloat scale = self.coverImg.frame.size.width/self.coverImg.frame.size.height;
        //獲取第一幀圖片的大小(可以根據(jù)這個(gè)判斷出來(lái)是橫屏視頻還是豎屏視頻)
        CGSize size = cover.size;
        CGFloat clipH = size.width/scale;
        
        //獲取按比例裁剪后的圖片
        UIImage *clipImage = [self getImageByCuttingImage:cover Rect:CGRectMake(0, size.height/2-clipH/2, size.width,clipH)];
        //3.獲取模糊圖片
        UIImage *dimImg = [self coreBlurImage:clipImage withBlurNumber:30.0f];
        
        //獲取模糊圖大小
        UIImage *bottom = dimImg;
        CGImageRef imgRef1 = bottom.CGImage;
        CGFloat w1 = CGImageGetWidth(imgRef1);
        CGFloat h1 = CGImageGetHeight(imgRef1);
        
        //4. 獲取放在封面中間的清晰圖片
        UIImage *clearImg = [self thumbnailWithImageWithoutScale:cover size:CGSizeMake((size.height/h1)*w1, h1)];
        //獲取清晰圖片大小
        CGImageRef imgRef = clearImg.CGImage;
        CGFloat w = CGImageGetWidth(imgRef);
        CGFloat h = CGImageGetHeight(imgRef);
        
        //5.合成圖片以模糊圖片大小為畫(huà)布創(chuàng)建上下文(以模糊圖片為底圖將清晰圖放到模糊圖中心)
        UIGraphicsBeginImageContext(CGSizeMake(w1, h1));
        [bottom drawInRect:CGRectMake(0, 0, w1, h1)];
        //把清晰圖畫(huà)到上下文中
        [clearImg drawInRect:CGRectMake(w1/2 - w/2, 0, w, h)];
        //6.獲取合成圖片
        UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//從當(dāng)前上下文中獲得最終圖片
        //關(guān)閉上下文
        UIGraphicsEndImageContext();
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 7. 改變UI
            self.coverImg.image = resultImg;
        });
        
    });
}


二. 橫屏視頻封面處理(用到的方法在文章最后)


- (void)landscapeBtnClick:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"landscape" ofType:@"mp4"];
    
    NSURL *url = [NSURL fileURLWithPath:path];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 1.獲取視頻第一幀圖片
        UIImage *cover = [self getVideoPreViewImage:url];
        
        //2.按照比例截取封面圖片
        
        //獲取coverImg(視頻封面的UI控件)的寬高比例用于截取圖片(橫屏這里截取的圖片保持高不變,以圖片中心為基準(zhǔn),按照coverImg的比例裁剪的寬,得到圖片)
        CGFloat scale = self.coverImg.frame.size.width/self.coverImg.frame.size.height;
        CGSize size = cover.size;
        CGFloat clipW = size.height*scale;
        
        UIImage *resultImg = [self getImageByCuttingImage:cover Rect:CGRectMake(size.width/2 - clipW/2 , 0, clipW, size.height)];
        dispatch_async(dispatch_get_main_queue(), ^{
          //3.修改UI
            self.coverImg.image = resultImg;
        });
    });  
}


三. 共用代碼部分

//截取視頻第一針
-(UIImage*)getVideoPreViewImage:(NSURL *)path
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
    AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    assetGen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return videoImage;
}
///裁剪正方形展示
-(UIImage *)getImageByCuttingImage:(UIImage *)image Rect:(CGRect)rect{
    
    //大圖bigImage
    
    //定義myImageRect,截圖的區(qū)域
    
    CGRect myImageRect = rect;
    
    UIImage* bigImage= image;
    
    CGImageRef imageRef = bigImage.CGImage;
    
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
    
    CGSize size;
    
    size.width = rect.size.width;
    
    size.height = rect.size.height;
    
    UIGraphicsBeginImageContext(size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextDrawImage(context, myImageRect, subImageRef);
    
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    
    UIGraphicsEndImageContext();
    
    return smallImage;
    
}

//保持原來(lái)的長(zhǎng)寬比,生成一個(gè)縮略圖

-(UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{
    
    UIImage *newimage;
    
    if (nil == image) {
        
        newimage = nil;
        
    }
    
    else{
        
        CGSize oldsize = image.size;
        
        CGRect rect;
        
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
            
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            
            rect.size.height = asize.height;
            
            rect.origin.x = (asize.width - rect.size.width)/2;
            
            rect.origin.y = 0;
            
        }
        
        else{
            
            rect.size.width = asize.width;
            
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            
            rect.origin.x = 0;
            
            rect.origin.y = (asize.height - rect.size.height)/2;
            
        }
        
        UIGraphicsBeginImageContext(asize);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        
        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
        
        [image drawInRect:rect];
        
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
    }
    
    return newimage;
    
}
//返回模糊圖效果
- (UIImage *)coreBlurImage:(UIImage *)image withBlurNumber:(CGFloat)blur{
    
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    //設(shè)置filter
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:@(blur) forKey:@"inputRadius"];
    //模糊圖片
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    //CGImageRef outImage = [context createCGImage:result fromRect:[result extent]];
    CIImage *im = [CIImage imageWithCGImage:image.CGImage];
    CGImageRef outImage = [context createCGImage: result fromRect:[im extent]];
    UIImage *blurImage = [UIImage imageWithCGImage:outImage];
    CGImageRelease(outImage);
    return blurImage;
    
}

四 . 項(xiàng)目下載地址:https://github.com/Bobo168/vedioCover

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,414評(píng)論 4 61
  • 炎炎夏日,酷熱難安?,F(xiàn)代人,尤其是像我一樣生活在深圳的人,已經(jīng)無(wú)法忍受沒(méi)有空調(diào)的日子。昨晚與幾位好友相約去大...
    二七1999閱讀 394評(píng)論 0 1
  • 關(guān)于生活,我不知道該說(shuō)些什么 一扇門(mén),兩個(gè)空間,兩個(gè)世界。 門(mén)里,一間小小的臥室,一張床,一個(gè)寫(xiě)字臺(tái),幾把椅子,一...
    梅厶藝閱讀 611評(píng)論 0 1

友情鏈接更多精彩內(nèi)容