• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为使用 AVAssetImageGenerator ios 生成的缩略图设置图像大小

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:58:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用 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];



Best Answer-推荐答案


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

AVAssetImageGenerator scales images such that they fit within the defined bounding box. Images are never scaled up. The aspect ratio of the scaled image is defined by the apertureMode property.

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

关于ios - 为使用 AVAssetImageGenerator ios 生成的缩略图设置图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23800500/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap