ios - 为使用 AVAssetImageGenerator ios 生成的缩略图设置图像大小
<p><p>我使用 <code>AVAssetImageGenerator</code> 类生成缩略图,但问题是我需要将该缩略图适合 <code>UITableViewCell</code> 具有 <code>UIImageView</code> 大小( 320,239)。
我将 <code>AVAssetImageGenerator</code> 最大尺寸属性指定为 (320,239) 但我没有得到我指定的所需尺寸请任何帮助提前谢谢。请查看以下代码以供快速引用。</p>
<pre><code> AVURLAsset *asset = [ initWithURL:self.consenteeVideoUrl options:nil];
AVAssetImageGenerator *gen = [ 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 = ;
UIImage *thumbnailImage = [ initWithCGImage:image];
//thumbnailImage = ;
CGImageRelease(image);
</code></pre>
<p>我从“stackoverflow”获得了另一个解决方案,但在我的场景中没有多大帮助。`</p>
<pre><code>AVURLAsset *asset=[ initWithURL:self.consenteeVideoUrl options:nil];
AVAssetImageGenerator *generator = [ 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 = [ initWithCGImage:im];
//save image to application directory...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *folderPath = ;
if (![ fileExistsAtPath:folderPath])
[ createDirectoryAtPath:folderPathwithIntermediateDirectories:NO attributes:nil error:&error];
NSString *savedImagePath = ];
NSData *imageData = UIImagePNGRepresentation(thumbnailImage);
BOOL success = ;
if (success) {
CSLog(@"sucess");
}
partnerEvent.thumbnailImageStr = ;
;
};
CGSize maxSize = CGSizeMake(320, 239);
generator.maximumSize = maxSize;
] completionHandler:handler];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的代码没有问题,但如文档中所述<br/></p>
<blockquote>
<p>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.<br/></p>
</blockquote>
<p>由于 <code>apertureMode</code> 设置为 <code>AVAssetImageGeneratorApertureModeProductionAperture</code> 生成的图像保持纵横比。如果视频和设置的尺寸具有相同的宽高比,它只会适合最大尺寸,而指定的尺寸不太可能发生这种情况。<br/>
尝试将 <code>UIImageView</code> <code>contentMode</code> 更改为 <code>UIViewContentModeScaleAspectFill</code> 一些内容可能会被裁剪,但您将填充整个 imageView</p></p>
<p style="font-size: 20px;">关于ios - 为使用 AVAssetImageGenerator ios 生成的缩略图设置图像大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23800500/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23800500/
</a>
</p>
页:
[1]