iosswiftxcodeavfoundationavcapture

how to get focal length of camera of iPhone using AVFoundation


how can I find focal length of iPhone camera lens. I already find out lens aperture value by this:- let aperture = currentcamera?.lensAperture
print("Aperture of camera is:-",aperture!)


Solution

  • To do this you need to first take an image from the camera, then read its focal-length property from the Exif data returned. Sample code is given here and copied below for reference:

    // delegate method for the image picker
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
        let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
        let exifdata = metadata!["{Exif}"] as! NSDictionary
    
        // focal length
        let focalLength = exifdata["FocalLength"] as! Double
    
        print("Focal length \(focalLength)")
    }