LOLXDXPLOL
LOLXDXPLOL

Reputation: 611

Using Lightsensor

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?

Upvotes: 0

Views: 475

Answers (1)

Tejas Ardeshna
Tejas Ardeshna

Reputation: 4371

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()
    }

}

Upvotes: 1

Related Questions