ios app 視頻開發

1、獲取視頻縮略圖

//-----------------------------------------------------
//獲取視頻縮略圖,如下兩個同爲截取視頻縮略圖,第一個爲同步,第二個爲異步
//-----------------------------------------------------
-(UIImage *)getImageWihtUrl:(NSString *)videoURL
{
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
//    NSURL *url = [[[NSURL alloc] initFileURLWithPath:videoURL] autorelease];
    NSURL *url=[[NSURL alloc]initWithString:videoURL];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    generator.appliesPreferredTrackTransform = YES;
    generator.maximumSize = CGSizeMake(240, 150);
    NSError *error = nil;
    CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(60, 10) actualTime:NULL error:&error];
    UIImage *image = [UIImage imageWithCGImage: img];
    return image;
}
-(void)generateImageWithUrl:(NSString *)theUrl andThumbnailName:(NSString *)theImgName
{
    
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL URLWithString:theUrl] options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
//    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(3,60);
    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
    {
        if (result == AVAssetImageGeneratorFailed)
        {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }
        if (result==AVAssetImageGeneratorSucceeded)
        {
            UIImage *theThumbnailsImage=[UIImage imageWithCGImage:im];
            [self saveToDocument:theThumbnailsImage withFilePath:[self pathForVideoThumbnails:theImgName]];
            imageLoadTimes++;
            if (imageLoadTimes%4==0)
            {
                [self reloadGridView];
            }
        }
//        [generator release];
    };
    
    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

2、將圖片保存到本地

//-----------------------------------------------------
//將視頻縮略圖保存到本地目錄,防止重複下載
//-----------------------------------------------------
- (UIImage *)getImageFromDocumentWithImgName:(NSString *)theImageName
{
    NSString *imagePath = [self pathForVideoThumbnails:theImageName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判斷文件是否存在
    if (![fileManager fileExistsAtPath:imagePath])
    {
//        NSLog(@"未取到圖片");
        return nil;
    }
    else
    {
        //從指定目錄讀取圖片
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
//        NSLog(@"取到圖片");
        return image;
    }
}
-(BOOL)saveToDocument:(UIImage *) image withFilePath:(NSString *) filePath
{
    if ((image == nil) || (filePath == nil) || [filePath isEqualToString:@""])
    {
        return NO;
    }
    @try {
        NSData *imageData = nil;
        //獲取文件擴展名
        NSString *extention = [filePath pathExtension];
        if ([extention isEqualToString:@"png"])
        {
            //返回PNG格式的圖片數據
            imageData = UIImagePNGRepresentation(image);
        }
        else
        {
            //返回JPG格式的圖片數據,第二個參數爲壓縮質量:0:best 1:lost
            imageData = UIImageJPEGRepresentation(image, 0);
        }
        if (imageData == nil || [imageData length] <= 0)
        {
            return NO;
        }
        //將圖片寫入指定路徑
        [imageData writeToFile:filePath atomically:YES];        
        return  YES;
    }
    @catch (NSException *exception)
    {
        NSLog(@"保存圖片失敗");
    }
    return NO;
    
}
-(NSString *)pathForVideoThumbnails:(NSString *)theImgStr
{
    //獲取Documents文件夾目錄
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [path objectAtIndex:0];
    //獲取文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //指定新建文件夾路徑
    NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"VideoImageFile"];
    //建立VideoImageFile文件夾
    [fileManager createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];
    //返回保存圖片的路徑(圖片保存在ImageFile文件夾下)
    NSString * imagePath = [imageDocPath stringByAppendingPathComponent:theImgStr];
    return imagePath;
}
-(void)removeViedoImageDoucumetForPath:(NSString *)thePath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [path objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *imageDocPath = [documentPath stringByAppendingPathComponent:thePath];
    [fileManager removeItemAtPath:imageDocPath error:nil];
}
//清空緩存
- (void)flushCache
{
    [SDWebImageManager.sharedManager.imageCache clearMemory];
    [SDWebImageManager.sharedManager.imageCache clearDisk];
}

3、push動畫

//-----------------------------------------------------
//push動畫
//-----------------------------------------------------
-(void)addPushAnimationInView:(UIView *)theView andAnimationDirection:(NSString *)theDirection
{
    CATransition *transtion = [CATransition animation];
    transtion.duration = 0.3;
    [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [transtion setType:kCATransitionPush];
    [transtion setSubtype:theDirection];
    [theView.layer addAnimation:transtion forKey:@"pushAnimation"];
}

4、使用html5控制播放器樣式

<video id="Video1" autoplay="autoplay" controls="controls" poster="" height="396" width="704" title="video element"></video>
autoplay:自動播放視頻
controls:設置播放控件,缺省則不顯示默認播放控件

video.removeAttribute("controls");//經過這種方式也能夠移除掉默認的播放控件

document.addEventListener("DOMContentLoaded", function (){init();}, false);//給視頻添加監遵從而實現各類功能
相關文章
相關標籤/搜索