ios - Swift record video using same method as taking photo -
i'm using following code allow users take photos:
private func configurephotoview() { capturedphoto.contentmode = .scaleaspectfill capturedphoto.clipstobounds = true capturedphoto.hidden = true capturesession = avcapturesession() capturesession!.sessionpreset = avcapturesessionpresetphoto photocapturedevice = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo) var error: nserror? photodeviceinput = avcapturedeviceinput(device: photocapturedevice, error: &error) if error == nil && capturesession!.canaddinput(photodeviceinput) { capturesession!.addinput(photodeviceinput) stillimageoutput = avcapturestillimageoutput() stillimageoutput!.outputsettings = [avvideocodeckey: avvideocodecjpeg] if capturesession!.canaddoutput(stillimageoutput) { capturesession!.addoutput(stillimageoutput) previewlayer = avcapturevideopreviewlayer(session: capturesession) previewlayer!.videogravity = avlayervideogravityresizeaspectfill previewlayer!.connection?.videoorientation = avcapturevideoorientation.portrait cameraview.layer.addsublayer(previewlayer) capturesession!.startrunning() self.view.addgesturerecognizer(uitapgesturerecognizer(target: self, action: "focusphoto:")) } } else { //todo: handle error } photooverlay.backgroundcolor = uicolor(white: 0, alpha: 0.5) }
when press button function gets called:
@ibaction func didpresstakephoto(sender: uibutton) { if let videoconnection = stillimageoutput!.connectionwithmediatype(avmediatypevideo) { videoconnection.videoorientation = avcapturevideoorientation.portrait stillimageoutput?.capturestillimageasynchronouslyfromconnection(videoconnection, completionhandler: {(samplebuffer, error) in if (samplebuffer != nil) { var imagedata = avcapturestillimageoutput.jpegstillimagensdatarepresentation(samplebuffer) var dataprovider = cgdataprovidercreatewithcfdata(imagedata) var cgimageref = cgimagecreatewithjpegdataprovider(dataprovider, nil, true, kcgrenderingintentdefault) self.capturedimage = uiimage(cgimage: cgimageref, scale: 1.0, orientation: uiimageorientation.right) self.capturedphoto.image = self.capturedimage } }) } }
this code lets me take photos whatever aspect ratio desire. there way can modify didpresstakephoto
code make video?
i can't find 1 swift tutorial on how make custom video recorder.
your avcapturesession
has 1 output, avcapturestillimageoutput
. if want capture video, need add either avcapturevideodataoutput
or avcapturemoviefileoutput
output capture session.
av foundation programming guide here: still , video media capture.
Comments
Post a Comment