我正在使用CodenameOne创建一个应用程序,并且必须使用ios本机界面来生成一些视频缩略图。这是我在iOS中使用的代码:
-(NSData*)createThumbnailFromMp4:(NSString*)param{
NSURL *mediaURL = [NSURL fileURLWithPath:param];
AVAsset *asset = [AVAsset assetWithURL:mediaURL];
CMTime duration = [asset duration];
CMTime snapshot = CMTimeMake(duration.value / 2, duration.timescale);
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:snapshot actualTime:nil error:nil];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return UIImagePNGRepresentation(thumbnail);
}
传入的参数是视频的完整路径,但是当我将其称为0长度byte []时,我得到的全部信息。这是CodenameOne中的代码:
byte[] thumbnailImage = tg.createThumbnailFromMp4(FileSystemStorage.getInstance().getAppHomePath() + movePath);
其中movePath只是本地存储视频的文件名。
最佳答案
AVAsset *asset = [AVAsset assetWithURL:urlvideo];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 1000 ;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
image1 = [UIImage imageWithCGImage:imageRef];