Reputation: 15512
I am wondering how I can get UIImage
from video stream?
What I have done :
First I run stream:
let player = AVPlayer(url: sourceURL)
player.play()
Then I check if asset
from AVPlayer
is not empty.
if let asset = player.currentItem?.asset {
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.requestedTimeToleranceBefore = CMTimeMake(1, 15)
imageGenerator.requestedTimeToleranceAfter = CMTimeMake(1, 15)
let time = CMTimeMake(1, 1)
let imageRef = try! imageGenerator.copyCGImage(at: time, actualTime: nil)
return UIImage(cgImage:imageRef)
}
I am using try!
for test purposes and I know that I am not handling exceptions.
But I receive error:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x170654250 {Error Domain=NSOSStatusErrorDomain Code=-12782 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12782), NSLocalizedDescription=The operation could not be completed}
Also I try this answers.
Upvotes: 2
Views: 838
Reputation: 631
I'm pretty sure if you remove the
requestedTimeToleranceBefore
and requestedTimeToleranceAfter
you will have no problems. Or change their property to kCMTimeZero
I would also suggest adding the following line
imageGenerator.appliesPreferredTrackTransform = true
Upvotes: 0