swift - Export video to the size of the UIImagePickerController view -
i have app user take video using uiimagepickercontroller.
then create watermark on top of recorded video , export avassetexportsession.
i'm guessing need use cgaffinetransformscale , transform export right size, i'm new transforming.
what kind of transform use crop video size of uiimagepickercontroller's view?
func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]) { let videopath = info[uiimagepickercontrollermediaurl] as! nsurl let stringvideopath = videopath.path //add watermark starting here let videoasset = avurlasset(url: videopath) let mixcomposition = avmutablecomposition() let compositionvideotrack = mixcomposition.addmutabletrackwithmediatype(avmediatypevideo, preferredtrackid: kcmpersistenttrackid_invalid) let clipvideotrack = videoasset.trackswithmediatype(avmediatypevideo)[0] { try compositionvideotrack.inserttimerange(cmtimerangemake(kcmtimezero, videoasset.duration), oftrack: clipvideotrack, attime: kcmtimezero) } catch { print("error") } compositionvideotrack.preferredtransform = clipvideotrack.preferredtransform //create watermark image let myimage = uiimage(named: "watermarkimage.png") let alayer = calayer() alayer.contents = myimage?.cgimage alayer.frame = cgrectmake(5, 25, 100, 60) alayer.opacity = 1.0 //sort layer let videosize = clipvideotrack.naturalsize let parentlayer = calayer() let videolayer = calayer() parentlayer.frame = cgrectmake(0, 0, videosize.height, videosize.width) videolayer.frame = cgrectmake(0, 0, videosize.height, videosize.width) parentlayer.addsublayer(videolayer) parentlayer.addsublayer(alayer) //create composition , add instructions insert layer let videocomp = avmutablevideocomposition() videocomp.rendersize = cgsize(width: videosize.height, height: videosize.width) videocomp.frameduration = cmtimemake(1, 30) videocomp.animationtool = avvideocompositioncoreanimationtool(postprocessingasvideolayer: videolayer, inlayer: parentlayer) //instructions let maininstruction = avmutablevideocompositioninstruction() maininstruction.timerange = cmtimerangemake(kcmtimezero, mixcomposition.duration) let videotrack = mixcomposition.trackswithmediatype(avmediatypevideo)[0] let layerinstruction = avmutablevideocompositionlayerinstruction(assettrack: videotrack) //i'm guessing have let zoomtransform = cgaffinetransformscale(compositionvideotrack.preferredtransform, 1, 1) , apply transform below instead of compositionvideotrack.preferredtransform layerinstruction.settransform(compositionvideotrack.preferredtransform, attime: kcmtimezero) maininstruction.layerinstructions = [layerinstruction] videocomp.instructions = [maininstruction] let assetexport = avassetexportsession(asset: mixcomposition, presetname: avassetexportpresetmediumquality) assetexport?.videocomposition = videocomp let exportpath = nstemporarydirectory().stringbyappendingstring("testvideo.mp4") let exporturl = nsurl(fileurlwithpath: exportpath) if nsfilemanager.defaultmanager().fileexistsatpath(exportpath) { { try nsfilemanager.defaultmanager().removeitematpath(exportpath)} catch{} } assetexport?.outputfiletype = avfiletypempeg4 assetexport?.outputurl = exporturl assetexport?.shouldoptimizefornetworkuse = true assetexport?.exportasynchronouslywithcompletionhandler({ () -> void in print("done") uisavevideoatpathtosavedphotosalbum(exporturl.path!, self, nil, nil) }) picker.dismissviewcontrolleranimated(true, completion: nil) picker.view.superview?.removefromsuperview() }
Comments
Post a Comment