【问题标题】:How can i set thumbnail image from video url which come from server [duplicate]如何从来自服务器的视频网址设置缩略图图像[重复]
【发布时间】:2016-10-08 04:32:46
【问题描述】:

我想在视频网址上​​显示缩略图。我必须这样做但不工作。它仅适用于本地文件路径,不适用于来自服务器 url。

视频网址:http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4

-(UIImage *)loadThumbNail:(NSURL *)urlVideo
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlVideo options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generate.appliesPreferredTrackTransform=TRUE;
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);
    return [[UIImage alloc] initWithCGImage:imgRef];
}

【问题讨论】:

  • 您可以做一件事,在上传视频时,您可以上传具有相同名称且带有 th_ 前缀的相同视频的缩略图图像,并且在显示视频时,您可以显示该缩略图图像,如果用户想要查看或下载视频,届时您可以流式传输或下载它。
  • @Kamlesh Shingarakhiya,你是怎么解决的?
  • 我使用下面的链接stackoverflow.com/questions/37343358/…第一个答案解决了它。
  • 是的,这需要一些时间,所以我以一定的时间间隔调用代码,这样我的 UI 就不会冻结。

标签: ios objective-c iphone video-thumbnails avurlasset


【解决方案1】:

使用此代码,

-(UIImage *)loadThumbNail:(NSURL *)urlVideo
{
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:urlVideo options:nil];

            if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
            {
                AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
                Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
                CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
                NSError *error;
                CMTime actualTime;


                CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];


                if (halfWayImage != NULL)
                {

                    NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
                    NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
                    NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);

                    UIImage *img=[UIImage imageWithCGImage:halfWayImage];


                    return img; //self.myimageView.image= img; 

                }

             }

 }

不要忘记框架,#import <AssetsLibrary/AssetsLibrary.h>,它对我有用,

希望对您有所帮助。

【讨论】:

    【解决方案2】:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
        return 1;
      }
    
     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return arrVideos.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    
    VideoListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
      if (cell == nil)
    {
        cell = [[VideoListingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    //fetch current object
    NSMutableDictionary *dict = [[arrVideos objectAtIndex:indexPath.row] mutableCopy];
    
    //set values
    if ([[dict valueForKey:kCaption] isEqual:[NSNull null]])
    {
        cell.lblCaption.text =@"No caption";
    }
    else
    {
            cell.lblCaption.text = [dict valueForKey:kCaption];
    }
    
    
    [cell.acivityView startAnimating];
    BOOL isNonEmpty = [CommonMethods isNonEmptyString:[dict valueForKey:kThumbnailURL]];
    
    if (isNonEmpty)
    {
        NSString *strUrl = [dict valueForKey:kThumbnailURL];
        [cell.imgThumbnail sd_setImageWithURL:[NSURL URLWithString:strUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [cell.acivityView stopAnimating];
        }];
    }
    else
    {
        [cell.imgThumbnail setImage:[UIImage imageNamed:@"img1"]];
        [cell.acivityView stopAnimating];
    }
     [cell.imgThumbnail setClipsToBounds:YES];
    
      return cell;
     }
    
       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     //fetch selected object
     NSMutableDictionary *dict = [[arrVideos objectAtIndex:indexPath.row]    mutableCopy];
    
      ExportVideoVC *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"ExportVideoVC"];
    obj.dictPost = [dict mutableCopy];
    [self presentViewController:obj animated:YES completion:nil];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
     return 124;
     }
    
         #pragma mark - webservice calls
    
       -(void)fetchAllVideos
      {
        //show progress hud
         [SVProgressHUD showWithStatus:kPleaseWait maskType:2];
    
        //prepare url
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"       video url ", kURL, kMediaType]];
    
       //ws call
         [[ServerCalls sharedObject] executeRequest:url withData:nil  method:kGET completionBlock:^(NSData *data, NSError *error) {
          if (error)
          {
              ShowAlertWithTitle(@"Please try again later", nil, kOK);
    
             //reload tableview
             [tblVideos reloadData];
          }
        else if (data)
         {
             NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
             BOOL isSuccess = ![[responseData allKeys] containsObject:kMessage];
            if (isSuccess)
            {
                arrVideos = [[responseData valueForKey:kQuickSpots] mutableCopy];
            }
            else
            {
                ShowAlertWithTitle(@"Please try again later", nil, kOK);
            }
        }
    
        //reload tableview
        [tblVideos reloadData];
    
        [SVProgressHUD dismiss];
      }];
      }
    

    这可能对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 2017-12-10
      • 2018-06-14
      • 2012-12-06
      • 1970-01-01
      • 2013-12-23
      • 2015-12-07
      • 2013-09-13
      相关资源
      最近更新 更多