您的位置:首页 > 产品设计 > UI/UE

IOS批量截取视频截图 UIImage mp4 IOS Video

2014-10-09 09:33 211 查看
IOS批量截取视频截图

    //生成截图
    NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSLog(@"path %@", path);
    for (int i =
0; i<[mItems count]; i++) {
        NSMutableDictionary *info = (NSMutableDictionary *)mItems[i]; //视频信息
        NSString *mediaPath;
        mediaPath = [[NSBundle mainBundle] pathForResource:[info objectForKey:@"mediaName"]
                                                    ofType:[info objectForKey:@"type"]];
        UIImage *image = [self getImage:mediaPath];
        [UIImageJPEGRepresentation(image, 1) writeToFile:[path stringByAppendingPathComponent:[NSString stringWithFormat:@"file%d.jpg", i]] atomically:YES];
    }

// 获取指定位置的视频的截屏    
-(UIImage *)getImage:(NSString *)videoURL{
    AVURLAsset *asset = [[AVURLAsset
alloc] initWithURL:[NSURL
fileURLWithPath:videoURL] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator
alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform =
YES;
    CMTime time =
CMTimeMakeWithSeconds(20.5,
600);  //  参数( 截取的秒数, 视频每秒多少帧)
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen
copyCGImageAtTime:time actualTime:&actualTime
error:&error];
    UIImage *thumb = [[UIImage
alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  视频 ui uiimage nsstring ios