You are going to see two images below. We are getting the image at first square on the left convert it CGImage make some manipulations on it at pixel level and place it 6th square. But at the moment we did inactivated manupulations means we just create a CGImage and convert it back to an UIImage and put it 6th square.
As you can see at top one the image is distorted. I am unable to find reason at the moment.
But sometimes it is OK as it is at second picture.
I lost some time on it and looking for an answer the problem.
http://www.celen.info/good.jpg
let cgImage = image.cgImage!
let data: NSData = (cgImage.dataProvider?.data)!
var pix2 = [UInt8](data as Data
let numComponents = 4
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
let colorspace = CGColorSpaceCreateDeviceGray()
let rgbData = CFDataCreate(kCFAllocatorDefault, pix2, lenght)!
let provider = CGDataProvider(data: rgbData)!
let resultCGImage = CGImage(width: size_w,
height: size_h,
bitsPerComponent: cgImage.bitsPerComponent,
bitsPerPixel: cgImage.bitsPerPixel,
bytesPerRow: size_w * numComponents,
space: colorspace,
bitmapInfo: bitmapInfo,
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent)!
let finalUIImage = UIImage(cgImage: resultCGImage)