ios - How do I know when generateCGImagesAsynchronously has completely finished? -
i have requirement take video, convert each frame , image , save these images disk. i'd use avassetimagegenerator
efficiency's sake, , have code similar following:
the issue don't know when all image generation complete, need take action once frames written disk. example:
assetgenerator.generatecgimagesasynchronously(fortimes: frametimes, completionhandler: { (requestedtime, image, actualtime, result, error) in // 1. keep reference each image // 2. wait until images generated // 3. process images set })
it's step 2 above that's tripping me up. imagine can try count number of times completion handler gets called, , trigger appropriate method when count equals number of frames.
but i'm wondering if there's way use api know when every frame has been processed? maybe i've missed? guidance or advice appreciated.
i progressively process images, won't able fit them in memory @ once anyway. sample video @ times using assetgenerator.copycgimageattime
.
but may oversample (repeating frames) or undersample (skipping frames). if care that, try using avassetreader
read frames in video:
let reader = try! avassetreader(asset: asset) let videotrack = asset.trackswithmediatype(avmediatypevideo)[0] // read video frames bgra let trackreaderoutput = avassetreadertrackoutput(track: videotrack, outputsettings:[string(kcvpixelbufferpixelformattypekey): nsnumber(unsignedint: kcvpixelformattype_32bgra)]) reader.addoutput(trackreaderoutput) reader.startreading() while let samplebuffer = trackreaderoutput.copynextsamplebuffer() { if let imagebuffer = cmsamplebuffergetimagebuffer(samplebuffer) { // have images cvimagebuffers/cvpixelbuffers process(imagebuffer) } }
this gives frames cvpixelbuffer
s. can convert them other types using code like this. if you're interested in timestamp of frames, call cmsamplebuffergetpresentationtimestamp(samplebuffer)
.
Comments
Post a Comment