0
- (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?

Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
  • 2
    How about instead of trying to set the image from your method. Just pass in a UIImage to the next view controller. And then in that viewcontroller use ```setImage``` to set that UIImage in the UIimageView? –  Jun 10 '15 at 08:07
  • 2
    Also maybe the image is nil or something? You should do some checks before passing it in to the next view controller. Check ```error``` before you send in the image. Also check if ```thumbnail != nil``` before passing it to the next controller. –  Jun 10 '15 at 08:09
  • 3
    It could be that the code is fine but AVAsset does not have the correct file path for the video and thus your not getting a thumbnail, thus why i think its important that you check the ```error``` and whether ```thumbnail``` is nil or not. –  Jun 10 '15 at 08:10
  • 1
    Good points. Will work on your suggestions and update soon. Thanks Dan – Arvin Sanmuga Rajah Jun 10 '15 at 08:16
  • 2
    When U load UIViewController from the storyboard, its view isn't loaded yet (i.e. viewDidLoad() isn't called). It means that controller.imageView is nil. Solution: create a standalone UIImage property in your target controller .h file and set it to the self.imageView.image in MainViewController's viewDidLoad() – David Jun 10 '15 at 08:30
  • 1
    @David Yeah I was thinking about that. So it makes sense to just pass the UIimage data and let the viewcontroller itself set the imageview. –  Jun 10 '15 at 09:06

4 Answers4

1

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]
Vikash Rajput
  • 445
  • 5
  • 21
0

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);
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

Try this hope it will help you:

controller.imageView.image = [thumbnail copy];

Rashad
  • 11,057
  • 4
  • 45
  • 73
Sumeet Bajaj
  • 224
  • 2
  • 11
0

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

}