2

我尝试从视频中读取一些帧。视频是 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);  
}
4

2 回答 2

7

要么使用

imageGenerator.maximumSize = CGSizeZero; 

要获得未缩放的图像或定义 640 像素的边界框,则保持纵横比

imageGenerator.maximumSize = CGSizeMake(640, 640);

希望有帮助!

于 2014-10-03T10:34:33.963 回答
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);  
}
于 2014-10-03T08:54:31.637 回答