0

I want to change some button, text and background colors when the environment of the user is dark. I thougt about using the built-in lightsensor of the iPhone that's used for auto brightness. I came to the idea because I noticed iBooks does it too. Someone knows how? I'm only a beginner, and sorry if I made language mistakes :s

Thank you in advance!

Edit:

There's another way to do this, I found this piece of code but it's in Objective-C... Can someone translate it to swift please?

LOLXDXPLOL
  • 611
  • 1
  • 6
  • 9

1 Answers1

1

Try this:

func captureOutput(captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBufferRef, fromConnection connection: AVCaptureConnection) {
    var metadataDict: CFDictionaryRef = CMCopyDictionaryOfAttachments(nil, sampleBuffer, kCMAttachmentMode_ShouldPropagate)
    var metadata: [NSObject : AnyObject] = NSMutableDictionary(dictionary: metadataDict)
    CFRelease(metadataDict)
    var exifMetadata: [NSObject : AnyObject] = metadata[kCGImagePropertyExifDictionary].mutableCopy()
    var brightnessValue: Float = exifMetadata[kCGImagePropertyExifBrightnessValue].floatValue()
    if brightnessValue > 0.5 {
        self.view.backgroundColor = UIColor.whiteColor()
    }
    else{
       self.view.backgroundColor = UIColor.blackColor()
    }

}
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • Do I first have to write "import ImageIO"? – LOLXDXPLOL Sep 23 '15 at 13:00
  • Thanks, and now for example if I want to read the brightness level to change the backgroundcolor, I have to write: "if brightnessValue > 0.5 { UIView.backgroundColor = UIColor.whiteColor }" – LOLXDXPLOL Sep 23 '15 at 13:08
  • I'll accept the answer when I get home and can test it!n Thank you! Could you answer to my previous comment too? @TejasArdeshna – LOLXDXPLOL Sep 23 '15 at 13:17
  • I'm testing the code now, but i get a bunch of errors... I figured maybe have to change something in this piece of code? – LOLXDXPLOL Sep 25 '15 at 15:38