1

ipad アプリで動画のスクリーンショットを撮りたいです。

SOで検索したところ、サンプルコードがたくさん見つかりました。私はすべてを試しましたが、何もうまくいかないようです。

私はこの方法をすべて試しました:


1)試してみてください:MPMoviePlayerController

- (void) previewWithPlayer:(NSString*)path image:(UIImageView*)imView
{
  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
  UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
  [player stop];
  [player release];
  
  imView.image = thumbnail;
}

2) 試してみてください: AVAssetImageGenerator - v1

- (void) generateImage:(NSString*)path image:(UIImageView*)imView
{
  AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:path]];
  AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
  CMTime time = CMTimeMake(1, 1);
  UIImage *thumbnail = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]];
  imView.image = thumbnail;
}

13) 試してみてください: AVAssetImageGenerator - v2

- (void) generateImage:(NSString*)path image:(UIImageView*)imView
{
  AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:path] options:nil];
  AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  generator.appliesPreferredTrackTransform=TRUE;
  [asset release];
  CMTime thumbTime = CMTimeMakeWithSeconds(2,30);
  
  AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
      NSLog(@"couldn't generate thumbnail, error:%@", error);
    }

    UIImage *thumbImg = [[UIImage imageWithCGImage:im] retain];
    imView.image = thumbImg;
    [generator release];
  };
  
  CGSize maxSize = CGSizeMake(320, 180);
  generator.maximumSize = maxSize;
  [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

しかし、何も機能しません。
MOV、MP4、何も試しませんでした。

パスは正しく、ビデオは機能しています。

NSString *fPath = [[NSBundle mainBundle] pathForResource:@"VideoA" ofType:@"mp4"];
NSLog(@"%@", fPath);
[self generateImage:fPath image:_ImgA];

何が問題なのですか?イメージ ビューには何も表示されず、エラーも返されません。
iOS は、iPad シミュレーター/デバイスで 6.0/5.1 です。

動画は854×480ピクセル、H.264、AACです。約30Mbのサイズ。

私はこの問題に夢中になっているので、私を助けてください。

ありがとう。


編集

デバイスでは、次のエラーが返されます:

サムネイルを生成できませんでした。エラー:エラー ドメイン=NSURLErrorDomain Code=-1 "不明なエラー" UserInfo=0x1e0a2f30 {NSUnderlyingError=0x1e0a3900 "操作を完了できませんでした。(OSStatus エラー -12935.)"、NSLocalizedDescription=不明なエラー}

4

1 に答える 1

6

解決しました。
トリック:fileURLWithPath:ではなくを使用しますURLWithString:。どうやら違いは本当に、本当に重要です。

ノアのおかげです。https://stackoverflow.com/a/4201419/88461

于 2012-11-20T15:00:44.173 に答える