은 내가 UIImageView
에 표시하는 UIImagePickerController
에서 선택한 비디오에서 첫 번째 프레임을 얻으려고하지만, 가능하다면 나도 몰라. 그렇다면 어떻게해야합니까?UIImagePickerController에서 비디오의 첫 번째 프레임을 가져 오는 중?
답변
다음 두 가지 방법 중 하나로이 작업을 수행 할 수 있습니다. 첫 번째 방법은 미리 잡아 위해 MPMoviePlayerController
을 사용하는 것입니다 :이 작동
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
timeOption:MPMovieTimeOptionNearestKeyFrame];
,하지만 MPMoviePlayerController
은 특히 경량의 대상이 아닌, 특히 빠른 썸네일을 잡아 없습니다.
선호되는 방법은 AVFoundation의 새로운 AVAssetImageGenerator
을 사용하는 것입니다. 이것은 기존 방식보다 빠르고 가볍고 유연합니다. 다음은 비디오에서 자동 축소 된 이미지를 반환하는 도우미 메서드입니다.
+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL
atTime:(NSTimeInterval)time
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@", igError);
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
return thumbnailImage;
}
비동기 사용
- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@", igError);
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
dispatch_async(dispatch_get_main_queue(), ^{
completion(thumbnailImage);
});
});
}
"Core Media"프레임 워크를 대상에 추가해야합니다. 에서는 videoURL이 fileURLWithPath 또는 URLWithString (에 의해 만들어진 경우는 NSString의 경로가있을 때 에주의 : –
한 내 동료가 ... 당신에게 이런 일 만 들으 쓰기이 사용하려고 다른위한 –
할 수 없습니다 *) – user1105951
- 1. UIImagePickerController에서 선택한 비디오의 크기를 가져 오기
- 2. 비디오 (flv)의 첫 번째 프레임을 가져 오는 PHP 코드
- 3. 비디오의 첫 번째 프레임 만로드하십시오.
- 4. UIImagePickerController에서 URL을 가져 오는 방법
- 5. 동적으로 첫 번째 프레임을 HTML5 동영상의 포스터로 사용하고 있습니까?
- 6. 소스 코드에서 비디오의 ID를 가져 오는 방법
- 7. 플래시에서 비디오의 프레임을 프로그래밍 방식으로 추출
- 8. SQL에서 문자열의 첫 번째 문자를 가져 오는 방법은 무엇입니까?
- 9. NSManagedObjectContext = nil을 가져 오는 첫 번째 시도 후 왜입니까?
- 10. 구성표 목록의 첫 번째 항목을 가져 오는 방법?
- 11. 첫 번째 문자 만 가져 오는 PWideChar 매개 변수가있는 함수
- 12. 코어 데이터에서 첫 번째 엔티티 만 가져 오는 방법은 무엇입니까?
- 13. LinqToEntities 결과의 첫 번째 요소를 가져 오는 방법은 무엇입니까?
- 14. 게시물의 첫 번째 이미지 URL을 가져 오는 중입니까?
- 15. 웹에서 두 번째 XML을 가져 오는 중 오류가 발생했습니다.
- 16. 컨트롤러에서 selectlist 첫 번째 값 가져 오기
- 17. 쿼리의 첫 번째 행에서 필드 가져 오기
- 18. 세 글자 중 하나와 일치하는 첫 번째 글자로 레코드를 가져 오는 방법
- 19. simplexml을 사용하여 첫 번째 값을 읽는 중
- 20. 첫 번째 WCF 서비스를 시작하는 중
- 21. 여러 클래스 중 첫 번째 선택
- 22. java의 웹 페이지에서 URL을 가져 오는 중
- 23. mysql 쿼리를 가져 오는 중
- 24. 클래스에서 값을 가져 오는 중
- 25. 문서 ID를 가져 오는 중
- 26. 창 ID를 가져 오는 중
- 27. 컨트롤의 HtmlTextWriter를 가져 오는 중
- 28. CImageList에서 CBitmap 가져 오는 중
- 29. 테이블에서 offsetTop을 가져 오는 중
- 30. 파일의 첫 번째 줄만 읽으시겠습니까?
비슷한 질문 http://stackoverflow.com/questions/1347562/getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk –