Q:

Applying Filters to image swap height and width in swift

currently i am working on an application where i capture photos and apply filters to photos. filters are applying perfectly but after applying filter new image size height width is swapped with captured image. like this 

i am unable to figure out where i am making mistake 
  let ciContext = CIContext(options: nil)
    let coreimg = CIImage(image: AppDelegate.captured_iamge!)
    let filter = CIFilter(name: CIFilterNames[indexPath.row])
    filter!.setDefaults()
    filter!.setValue(coreimg, forKey: kCIInputImageKey)
    let filtered_img_data = filter!.value(forKey: kCIOutputImageKey) as! CIImage
    let filtered_img_ref = ciContext.createCGImage(filtered_img_data, from: filtered_img_data.extent)
    let img = UIImage(cgImage: filtered_img_ref!)
    img_view.image = img

if someone can help it would be great 

A:

This is likely happening because your input image has a different orientation (portrait) then the natural orientation of your camera (landscape).
If you ask an UIImage for its size, it already factors in its orientation. Under the hood, though, the image data is stored as it came from the camera, and that's also what Core Image is operating on. So in your filter chain, the image is loosing its orientation information.
I think you can fix that by passing that information to the output image on creation:
let inputImage = AppDelegate.captured_iamge!
//...
let img = UIImage(cgImage: filtered_img_ref!, scale: inputImage.scale, orientation: inputImage.orientation)

