如何设置AVAssetImageGenerator的大小?

时间:2014-10-03 08:51:29

标签: objective-c avfoundation avassetimagegenerator

我尝试从视频中读取一些帧。视频是640 x 480,但我得到的图像只有480 x 360.有没有办法获得原始尺寸的图像?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
        [self readMovieFrames:[info objectForKey:UIImagePickerControllerMediaURL]];

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)readMovieFrames:(NSURL *)url
{
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:playerItem.asset];
    imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;
    imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
    imageGenerator.appliesPreferredTrackTransform = YES;   
    //…
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:requestTime actualTime:&actualTime error:&error];
    UIImage *img = [UIImage imageWithCGImage:imageRef];
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
    CGImageRelease(imageRef);  
}

2 个答案:

答案 0 :(得分:3)

使用

imageGenerator.maximumSize = CGSizeZero; 

要获得未缩放的图像或定义640px的边界框,长宽比仍为

imageGenerator.maximumSize = CGSizeMake(640, 640);

希望有所帮助!

答案 1 :(得分:0)

- (void)readMovieFrames:(NSURL *)url
{
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:playerItem.asset];
imageGenerator.maximumSize = CGSizeMake(320, 239);
 // Add this line
    imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;
    imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
    imageGenerator.appliesPreferredTrackTransform = YES;   
    //…
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:requestTime actualTime:&actualTime error:&error];
    UIImage *img = [UIImage imageWithCGImage:imageRef];
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
    CGImageRelease(imageRef);  
}