无法在另一个视图控制器中显示缩略图

时间:2015-06-10 08:05:11

标签: ios objective-c avfoundation thumbnails cgimageref

- (void)displayThumbnail
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[self outputURL] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(1, 30);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef imageref = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imageref];
    CGImageRelease(imageref);

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    MainViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];

    controller.imageView.image = thumbnail;
}

我正在尝试在另一个视图控制器中显示录制视频的缩略图,但没有显示任何内容。我错过了什么吗?

4 个答案:

答案 0 :(得分:1)

在MainViewController.h中获取UIImage类型的属性

@property(strong,nonatomic) UIImage* thumbnailImg;
MainViewController.m中的

self.imageView.image = self.thumbnailImg;

设置此属性

[controller setThumbnailImg: thumbnail]

答案 1 :(得分:0)

我认为您存储在[self outputURL]中的路径是错误的。你可以在这里粘贴视频路径吗?

试试这段代码:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[self outputURL] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
int time = CMTimeGetSeconds ([asset duration]) /2;
NSError *error = nil;
CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:CMTimeMake(time, 1) actualTime:&actualTime error:&error];
thumb = [UIImage imageWithCGImage:image];
[UIImagePNGRepresentation(thumb) writeToFile:imgPath atomically:YES];
        CGImageRelease(image);

答案 2 :(得分:0)

试试这个希望它会对你有所帮助:

controller.imageView.image = [thumbnail copy];

答案 3 :(得分:0)

解决了!

大卫和丹的信誉:)

MainViewController.h

@property (weak, nonatomic) IBOutlet UIImageView *thumbnailAV;

MainViewController.m

  AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:[self outputURL] options:nil];
  AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
  generate1.appliesPreferredTrackTransform = YES;
  NSError *err = NULL;
  CMTime time = CMTimeMake(1, 2);
  CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
  UIImage *one = [[UIImage alloc] initWithCGImage:oneRef];

  [self.thumbnailAV setImage:one];    
  [self.view setFrame:CGRectMake(0, 0, 320, 422)];
  self.thumbnailAV.contentMode = UIViewContentModeScaleAspectFit;
}

THCaptureViewController.m

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

 MainViewController *controller= [mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];
        [self presentViewController:controller animated:YES completion:nil];

}