我一直在尝试从我从parse.com查询的视频中设置缩略图 但没有运气。这是我的缩略图代码:
- (UIImage *)thumbnailFromVideoAtURL:(NSURL *)url
{
AVAsset *asset = [AVAsset assetWithURL:url];
// Get thumbnail at the very start of the video
CMTime thumbnailTime = [asset duration];
thumbnailTime.value = 0;
// Get image from the video at the given time
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
}
它适用于普通视图控制器中的viewDidLoad
,但我不知道如何在单元格的UITableViewController
中设置它。有人能指出我正确的方向吗?
答案 0 :(得分:1)
在UITableViewDataSource
协议实施中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"yourellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
NSURL* videoURL = videos[indexPath.row];
...
[cell loadPhotoFromURL: videoURL];
...
return cell;
}
您必须实现自定义单元格:
CustomCell.h
@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *thumbnaliImageView;
CustomCell.m
#pragma mark - thumbnal loading
- (void)loadPhotoFromURL:(NSURL*)url
{
[self.thumbnaliImageView setBackgroundColor:[UIColor whiteColor]];
UIImage *image = [UIImage imageNamed:@"Stub"];
self.thumbnaliImageView.image = image;
self.thumbnaliImageView.contentMode = UIViewContentModeScaleAspectFit;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
AVAsset *asset = [AVAsset assetWithURL:url];
// Get thumbnail at the very start of the video
CMTime thumbnailTime = [asset duration];
thumbnailTime.value = 0;
// Get image from the video at the given time
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
if(thumbnail){
// do UI stuff back in UI land
dispatch_async(dispatch_get_main_queue(), ^{
self.thumbnaliImageView.image = thumbnail;
});
}
});
}
}
答案 1 :(得分:0)
使用CustomCell
static NSString *CellIdentifier = @"Cell";
CustomCell *cell;
if(indexPath.row != [Array count] )
{
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSString *imageurl = [NSString stringWithFormat:@"%@",[[arraysearchVideoList objectAtIndex:indexPath.row] valueForKey:@"imageUrl"]];
[cell.ImageView setImageWithURL:[NSURL URLWithString:imageurl]
placeholderImage:[UIImage imageNamed:@"noimage.png"]];
[cell.ImageView addSubview:image];
答案 2 :(得分:0)
你可以这样做, 在其中创建自定义单元格,以提供imageview标记 并且在表格的cellForRowAtIndexPath方法中应用此代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1;
CellIdentifier1 = @"contactList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CCell" owner:self options:nil];
cell = [ar objectAtIndex:1];
}
UIImageView *imgContact = (UIImageView*)[cell viewWithTag:TAG_CONTACTIMAGE]
imgContact.image = [self thumbnailFromVideoAtURL:url];
}