2013-03-12 10 views
13

Zaimplementowałem AVPlayer i chcę zrobić zdjęcie lub miniaturę po kliknięciu przycisku paska narzędzi i otworzyć w nowym UIViewController z UIImageView. Obraz powinien być skalowany dokładnie tak, jak AVPlayer. Już działa segue, muszę tylko zaimplementować, aby uzyskać obraz w bieżącym czasie odtwarzania.Utwórz miniaturę lub obraz odtwarzacza AVPlayer w bieżącym czasie

Dzięki!

Odpowiedz

34

Cel C

AVAsset *asset = [AVAsset assetWithURL:sourceURL]; 
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; 
CMTime time = CMTimeMake(1, 1); 
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; 
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); // CGImageRef won't be released by ARC 

Swift

var asset = AVAsset.assetWithURL(sourceURL) 
var imageGenerator = AVAssetImageGenerator(asset: asset!) 
var time = CMTimeMake(1, 1) 
var imageRef = try! imageGenerator!.copyCGImageAtTime(time, actualTime: nil) 
var thumbnail = UIImage.imageWithCGImage(imageRef) 
CGImageRelease(imageRef) // CGImageRef won't be released by ARC 

Swift 3,0

var sourceURL = URL(string: "Your Asset URL") 
var asset = AVAsset(url: sourceURL!) 
var imageGenerator = AVAssetImageGenerator(asset: asset) 
var time = CMTimeMake(1, 1) 
var imageRef = try! imageGenerator.copyCGImage(at: time, actualTime: nil) 
var thumbnail = UIImage(cgImage:imageRef) 

Uwaga: Interpretuj kod Swift zgodnie z twoją szybką wersją.

+0

Wykonanie samego ekranu działa przy użyciu obu metod, ale problem polega na tym, że zrzut ekranu jest skalowany jako pełny ekran, a nie w tym samym formacie, co moje wideo. Czy istnieje możliwość dostosowania rozmiaru "miniatury"? – kchromik

+1

Daje mi AVErrorOperationNotSupportedForAsset kod błędu. Próbuję załadować adres URL serwera m3u8. Proszę pomóż – ruyamonis346

+1

załadować plik m3u8, który wymaga 'AVPlayer', który jest w stanie odtwarzać plik streaming .m3u8. spróbuj zagrać za pomocą 'AVPlayer'. –

3

Spróbuj

- (UIImage*)takeScreeenShot { 

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL 
options:nil]; 

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 

imageGenerator.appliesPreferredTrackTransform = YES; 

NSError *err = NULL; 

CMTime time = CMTimeMake(1, 60); // time range in which you want 
screenshot 

CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL 
error:&err]; 

return [[UIImage alloc] initWithCGImage:imgRef]; 

} 

Hope this helps !!!

+0

Wystąpił błąd z powodu "generatora obrazu". Mówi, że jest niezgłoszona. – kchromik

+0

tat był literówką ... poprawiono go nw !!! –

1

Swift 2.x:

let asset = AVAsset(...) 
let imageGenerator = AVAssetImageGenerator(asset: asset) 
let screenshotTime = CMTime(seconds: 1, preferredTimescale: 1) 
if let imageRef = try? imageGenerator.copyCGImageAtTime(screenshotTime, actualTime: nil) { 

    let image = UIImage(CGImage: imageRef) 

    // do something with your image 
} 
+0

To działa, ale otrzymasz obróconą miniaturę. Dodaj imageGenerator.appliesPreferredTrackTransform = true –

0

Dodaj poniżej kod, aby wygenerować miniaturkę z video.

AVURLAsset *assetURL = [[AVURLAsset alloc] initWithURL:partOneUrl options:nil]; 

AVAssetImageGenerator *assetGenerator = [[AVAssetImageGenerator alloc] initWithAsset:assetURL]; 

assetGenerator.appliesPreferredTrackTransform = YES; 

NSError *err = NULL; 

CMTime time = CMTimeMake(1, 2);  

CGImageRef imgRef = [assetGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 

UIImage *one = [[UIImage alloc] initWithCGImage:imgRef];  
Powiązane problemy