ios - Objective-C AVCaptureDevice Front Camera -
i have followed tutorial guided through way make custom simple camera app, needs of use it. have 2 issues need changing focus on first 1 now.
the following code allows use of camera, need changed can use front camera. link here video sourced give them credit, , followed 1 of commenters said using front camera answer didn't @ all.
https://www.youtube.com/watch?v=xv1ffqvy-km
i'm not excellent @ coding @ all, trying learn. appreciated! many thanks.
@interface viewcontroller () @end @implementation viewcontroller avcapturesession *session; avcapturestillimageoutput *stillimageoutput; - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)viewwillappear:(bool)animated { session = [[avcapturesession alloc] init]; [session setsessionpreset:avcapturesessionpresetphoto]; avcapturedevice *inputdevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; nserror *error; avcapturedeviceinput *deviceinput = [avcapturedeviceinput deviceinputwithdevice:inputdevice error:&error]; if ([session canaddinput:deviceinput]) { [session addinput:deviceinput]; } avcapturevideopreviewlayer *previewlayer = [[avcapturevideopreviewlayer alloc] initwithsession:session]; [previewlayer setvideogravity:avlayervideogravityresizeaspectfill]; calayer *rootlayer = [[self view] layer]; [rootlayer setmaskstobounds:yes]; cgrect frame = frameforcapture.frame; [previewlayer setframe:frame]; [rootlayer insertsublayer:previewlayer atindex:0]; stillimageoutput = [[avcapturestillimageoutput alloc] init]; nsdictionary *outputsettings = [[nsdictionary alloc] initwithobjectsandkeys:avvideocodecjpeg, avvideocodeckey, nil]; [stillimageoutput setoutputsettings:outputsettings]; [session addoutput:stillimageoutput]; [session startrunning]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)takephoto:(id)sender { avcaptureconnection *videoconnection = nil; (avcaptureconnection *connection in stillimageoutput.connections) { (avcaptureinputport *port in [connection inputports ]) { if ([[port mediatype] isequal:avmediatypevideo]) { videoconnection = connection; break; } } } [stillimageoutput capturestillimageasynchronouslyfromconnection:videoconnection completionhandler:^(cmsamplebufferref imagedatasamplebuffer, nserror *error) { if (imagedatasamplebuffer != null) { nsdata *imagedata = [avcapturestillimageoutput jpegstillimagensdatarepresentation:imagedatasamplebuffer]; uiimage *image = [uiimage imagewithdata:imagedata]; imageview.image = image; } }]; } @end
this code returns avcapturedevice instance default device of given media type.
avcapturedevice *inputdevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
change code to
.... avcapturedevice *inputdevice = nil; nsarray *devices = [avcapturedevice deviceswithmediatype:avmediatypevideo]; for(avcapturedevice *camera in devices) { if([camera position] == avcapturedevicepositionfront) { // front camera inputdevice = camera; break; } } ......
Comments
Post a Comment