gpt4 book ai didi

iphone - 视频的缩略图

转载 作者:行者123 更新时间:2023-12-03 18:21:15 25 4
gpt4 key购买 nike

我正在从库中选择视频剪辑。我想创建它的缩略图。我已经应用了这个代码。但图像出现旋转。我想要它的原始 View 。

- (UIImage*)testGenerateThumbNailDataWithVideo {

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
[generate release];
NSLog(@"err==%@, imageRef==%@", err, imgRef);
UIImage *currentImg = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];
static BOOL flag = YES;
if (flag) {
NSData *tmpData = UIImageJPEGRepresentation(currentImg, 0.8);
NSString *path = [NSString stringWithFormat:@"%@thumbNail.png", NSTemporaryDirectory()];
BOOL ret = [tmpData writeToFile:path atomically:YES];
NSLog(@"write to path=%@, flag=%d", path, ret);
flag = NO;
}
return currentImg;
}

最佳答案

尝试使用AVAssetImageGenerator反而。 Apple 讨论使用 AVAssetImageGenerator 创建缩略图 here 。这是示例代码,它获取单个缩略图。您需要包含 AVFoundation 框架。并添加CoreMedia框架

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
[gen release];

还有一个解决方案是

-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};

CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

}

或者

ALAsset
display image from URL retrieved from ALAsset in iPhone

关于iphone - 视频的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8906004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com