无法在另一个视图控制器中显示缩略图
Unable to display thumbnail in another view controller
- (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;
}
我试图在另一个视图控制器中显示录制视频的缩略图,但没有显示任何内容。我错过了什么吗?
在MainViewController.h
中取属性的UIImage类型
@property(strong,nonatomic) UIImage* thumbnailImg;
在MainViewController.m
self.imageView.image = self.thumbnailImg;
设置这个属性
[controller setThumbnailImg: thumbnail]
我认为你存储在 [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);
试试这个希望对你有帮助:
controller.imageView.image = [thumbnail copy];
解决了!
感谢 David 和 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];
}
- (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;
}
我试图在另一个视图控制器中显示录制视频的缩略图,但没有显示任何内容。我错过了什么吗?
在MainViewController.h
中取属性的UIImage类型@property(strong,nonatomic) UIImage* thumbnailImg;
在MainViewController.m
self.imageView.image = self.thumbnailImg;
设置这个属性
[controller setThumbnailImg: thumbnail]
我认为你存储在 [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);
试试这个希望对你有帮助:
controller.imageView.image = [thumbnail copy];
解决了!
感谢 David 和 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];
}