Unable to display thumbnail in another view controller

176 views Asked by At
- (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;
}

I'm attempting to display a thumbnail of a recorded video in another view controller, but nothing shows up. Am I missing something?

4

There are 4 answers

0
Arvin Sanmuga Rajah On BEST ANSWER

Solved it !

Credits to David and Dan :)

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];

}
0
Vikash Rajput On

Take property of UIImage type in MainViewController.h

@property(strong,nonatomic) UIImage* thumbnailImg;

in MainViewController.m

self.imageView.image = self.thumbnailImg;

set this property

[controller setThumbnailImg: thumbnail]
5
Bhadresh Mulsaniya On

I think your path is wrong which you have stored in [self outputURL]. Can you paste video path here?

Try this code:

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);
0
Sumeet Bajaj On

Try this hope it will help you:

controller.imageView.image = [thumbnail copy];