一、获取视频缩略图
//-----------------------------------------------------
//获取视频缩略图,以下两个同为截取视频缩略图,第一个为同步,第二个为异步
//-----------------------------------------------------
-(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];
}
二、将图片保存到本地
//-----------------------------------------------------
//将视频缩略图保存到本地目录,防止重复下载
//-----------------------------------------------------
- (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];
}
三、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"];
}
四、使用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);//给视频添加监听从而实现各种功能