2

我正在使用AVAssetImageGenerator生成缩略图的类,但问题是我需要将该缩略图调整为UITableViewCell具有UIImageView大小(320,239)。我AVAssetImageGenerator将最大尺寸属性指定为 (320,239) 但我没有得到我指定的所需尺寸请任何帮助提前谢谢。请查看以下代码以供快速参考。

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.consenteeVideoUrl options:nil];

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];

gen.maximumSize = CGSizeMake(320, 239);

gen.apertureMode = AVAssetImageGeneratorApertureModeProductionAperture;

gen.appliesPreferredTrackTransform = YES;

CMTime time = CMTimeMakeWithSeconds(0.0, 600);

NSError *error = nil;

CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:image];
//thumbnailImage = [CSUtilities imageWithImage:thumbnailImage scaledToSize:CGSizeMake(320, 239)];
CGImageRelease(image);

我从“stackoverflow”获得的另一种解决方案,但在我的场景中没有多大帮助。`

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.consenteeVideoUrl options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;

CMTime thumbTime = CMTimeMakeWithSeconds(30,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }

    UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:im];
    //save image to application directory...
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:self.uniqueCodeString];
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath  withIntermediateDirectories:NO attributes:nil error:&error];

    NSString *savedImagePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",self.uniqueCodeString]];
    NSData *imageData = UIImagePNGRepresentation(thumbnailImage);
    BOOL success = [imageData writeToFile:savedImagePath atomically:YES];
    if (success) {
        CSLog(@"sucess");
    }
    partnerEvent.thumbnailImageStr = [self getImagePath];
    [self.ibPartnerEventTableView reloadData];
 };

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

1 回答 1

3

您的代码没有任何问题,但如文档中所述

AVAssetImageGenerator 缩放图像,使其适合定义的边界框。图像永远不会放大。缩放图像的纵横比由孔径模式属性定义。

由于apertureMode设置为AVAssetImageGeneratorApertureModeProductionAperture生成的图像保持纵横比。如果视频和尺寸集具有相同的纵横比,它将仅适合最大尺寸,并且在指定的尺寸下不太可能发生这种情况。
尝试将其更改为可以裁剪的UIImageView contentMode某些UIViewContentModeScaleAspectFill内容,但您将填充整个 imageView

于 2014-05-22T08:00:08.320 回答