iOS : AVFoundation Image Capture Dark -
i'm developping app captures photos ipad front camera. photos coming dark. have idea how fix issue, please ?
here code , explainations :
1) initialize capture session
-(void)viewdidappear:(bool)animated{ capturesession = [[avcapturesession alloc] init]; nsarray *devices = [avcapturedevice devices]; avcapturedevice *frontcamera; (avcapturedevice *device in devices){ if ([device position] == avcapturedevicepositionfront) { frontcamera = device; } } if ([frontcamera isexposuremodesupported:avcaptureexposuremodecontinuousautoexposure]){ nserror *error=nil; if ([frontcamera lockforconfiguration:&error]){ frontcamera.exposuremode = avcaptureexposuremodecontinuousautoexposure; frontcamera.focusmode=avcapturefocusmodeautofocus; [frontcamera unlockforconfiguration]; } } nserror *error = nil; avcapturedeviceinput *frontfacingcameradeviceinput = [avcapturedeviceinput deviceinputwithdevice:frontcamera error:&error]; [capturesession addinput:frontfacingcameradeviceinput]; [capturesession setsessionpreset:avcapturesessionpresethigh]; capturevideooutput = [[avcapturevideodataoutput alloc] init]; captureimageoutput =[[avcapturestillimageoutput alloc] init]; [capturesession addoutput:capturevideooutput]; [capturesession addoutput:captureimageoutput]; }
2) when user presses button record, starts timer , preview content of camera preview layer
- (ibaction)but_record:(uibutton *)sender { mainint = 4; timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(countup) userinfo:nil repeats:yes]; previewlayer = [[avcapturevideopreviewlayer alloc]initwithsession:capturesession]; previewlayer.connection.videoorientation = avcapturevideoorientationportrait; cgrect rect = cgrectmake(0, 0, self.aview.bounds.size.width, self.aview.bounds.size.height); previewlayer.frame = rect; [self.aview.layer addsublayer:previewlayer]; [capturesession startrunning]; }
3) @ end of timer, photo taken , saved
- (void)countup { mainint -=1; if (mainint == 0) { [timer invalidate]; timer = nil; [capturesession stoprunning]; avcaptureconnection *videoconnection = nil; (avcaptureconnection *connection in captureimageoutput.connections) { (avcaptureinputport *port in [connection inputports]) { if ([[port mediatype] isequal:avmediatypevideo] ) { videoconnection = connection; break; } } if (videoconnection) { break; } } [captureimageoutput capturestillimageasynchronouslyfromconnection:videoconnection completionhandler: ^(cmsamplebufferref imagesamplebuffer, nserror *error) { cfdictionaryref exifattachments = cmgetattachment( imagesamplebuffer, kcgimagepropertyexifdictionary, null); nsdata *imagedata = [avcapturestillimageoutput jpegstillimagensdatarepresentation:imagesamplebuffer]; stillimage = [[uiimage alloc] initwithdata:imagedata]; }]; [capturesession startrunning]; [capturesession stoprunning]; } }
4) finally, when user press save button, image recorded in specific album
- (ibaction)but_save:(uibutton *)sender { uiimage *img = stillimage; [self.library saveimage:img toalbum:@"myspecificalbum" withcompletionblock:^(nserror *error)]; }
in fact, code works resulting images dark...
this happening me , turned out trying capture , camera didn't have enough time stabilize. had add 0.5 seconds of delay before pictures normal brightness.
hth
Comments
Post a Comment