This function is supposed to take an image and convert it into data and from the data it will be converted back to image. when I used an image from an image library or when I take the photo in landscape mode it works fine. But when I take the photo in portrait mode the image will be rotated and stretched when its being converted back to an image. Is there any way to prevent that from happening?
func pixelCalculator (){
let image = originalImage
let height = Int((image.size.height))
print(height)
let width = Int((image.size.width))
let bitsPerComponent = Int(8)
let bytesPerRow = 4 * width
let colorSpace = CGColorSpaceCreateDeviceRGB()
let rawData = UnsafeMutablePointer<RGBAPixel>.allocate(capacity: (width * height))
let bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue
let CGPointZero = CGPoint(x: 0, y: 0)
let rect = CGRect(origin: CGPointZero, size: (image.size))
let imageContext = CGContext(data: rawData, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)
imageContext?.draw(image.cgImage!, in: rect)
let pixels = UnsafeMutableBufferPointer<RGBAPixel>(start: rawData, count: width * height)
let outContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: bitsPerComponent,bytesPerRow: bytesPerRow,space: colorSpace,bitmapInfo: bitmapInfo,releaseCallback: nil,releaseInfo: nil)
let outImage = UIImage(cgImage: outContext!.makeImage()!)
imageView.image = outImage
}