iOS:将CIFilter应用于UIImage时,结果图像会旋转

4

当我将CIFilter应用于从后置相机捕获的图像时,结果是完美的。但是,当我将CIFilter应用于从前置相机捕获的图像时,结果不好,因为图像旋转了。有什么问题吗?

我的滤镜函数如下:

func ApplyFilter()->UIImage
{
    let ciContext = CIContext(options: nil)
    let coreImage = CIImage(image: myImage!)
    let filter = CIFilter(name: "CIPhotoEffectNoir" )
    filter!.setDefaults()
    filter!.setValue(coreImage, forKey: kCIInputImageKey)
    let filteredImageData = filter!.valueForKey(kCIOutputImageKey) as! CIImage
    let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent)
    var newImage = UIImage(CGImage: filteredImageRef);

    return newImage

}

2
可能是使用CoreImage对图像进行滤镜处理会导致图像旋转的重复问题。 - Fabio Berger
1
当我将CIFilter应用于从后置摄像头捕获的图像时,结果是完美的,但是当我将CIFilter应用于从前置摄像头捕获的图像时,结果不好,因为图像被旋转了。 - Zee Janjua
3个回答

7
那是因为你的前置摄像头捕捉到了一个翻转的图像,就像镜子一样。你需要在捕捉图像后修正图像方向。 Swift 3
extension UIImage {

func fixOrientation() -> UIImage {

    // No-op if the orientation is already correct
    if ( self.imageOrientation == UIImageOrientation.up ) {
        return self;
    }

    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    var transform: CGAffineTransform = CGAffineTransform.identity

    if ( self.imageOrientation == UIImageOrientation.down || self.imageOrientation == UIImageOrientation.downMirrored ) {
        transform = transform.translatedBy(x: self.size.width, y: self.size.height)
        transform = transform.rotated(by: CGFloat(Double.pi))
    }

    if ( self.imageOrientation == UIImageOrientation.left || self.imageOrientation == UIImageOrientation.leftMirrored ) {
        transform = transform.translatedBy(x: self.size.width, y: 0)
        transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
    }

    if ( self.imageOrientation == UIImageOrientation.right || self.imageOrientation == UIImageOrientation.rightMirrored ) {
        transform = transform.translatedBy(x: 0, y: self.size.height);
        transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
    }

    if ( self.imageOrientation == UIImageOrientation.upMirrored || self.imageOrientation == UIImageOrientation.downMirrored ) {
        transform = transform.translatedBy(x: self.size.width, y: 0)
        transform = transform.scaledBy(x: -1, y: 1)
    }

    if ( self.imageOrientation == UIImageOrientation.leftMirrored || self.imageOrientation == UIImageOrientation.rightMirrored ) {
        transform = transform.translatedBy(x: self.size.height, y: 0);
        transform = transform.scaledBy(x: -1, y: 1);
    }

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    let ctx: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
                                   bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
                                   space: self.cgImage!.colorSpace!,
                                   bitmapInfo: self.cgImage!.bitmapInfo.rawValue)!;

    ctx.concatenate(transform)

    if ( self.imageOrientation == UIImageOrientation.left ||
        self.imageOrientation == UIImageOrientation.leftMirrored ||
        self.imageOrientation == UIImageOrientation.right ||
        self.imageOrientation == UIImageOrientation.rightMirrored ) {
        ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
    } else {
        ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
    }

    // And now we just create a new UIImage from the drawing context and return it
    return UIImage(cgImage: ctx.makeImage()!)
}

}

作为使用方式

let myImage = picCaptured.fixOrientation()

然后将CIFilter应用于该图像


3

0

Abdul Waheed的Swift 5.5解决方案:

func fixOrientation() -> UIImage {
// No-op if the orientation is already correct
if ( self.imageOrientation == UIImage.Orientation.up ) {
  return self;
}

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform: CGAffineTransform = CGAffineTransform.identity

if ( self.imageOrientation == UIImage.Orientation.down || self.imageOrientation == UIImage.Orientation.downMirrored ) {
  transform = transform.translatedBy(x: self.size.width, y: self.size.height)
  transform = transform.rotated(by: CGFloat(Double.pi))
}

if ( self.imageOrientation == UIImage.Orientation.left || self.imageOrientation == UIImage.Orientation.leftMirrored ) {
  transform = transform.translatedBy(x: self.size.width, y: 0)
  transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
}

if ( self.imageOrientation == UIImage.Orientation.right || self.imageOrientation == UIImage.Orientation.rightMirrored ) {
  transform = transform.translatedBy(x: 0, y: self.size.height);
  transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
}

if ( self.imageOrientation == UIImage.Orientation.upMirrored || self.imageOrientation == UIImage.Orientation.downMirrored ) {
  transform = transform.translatedBy(x: self.size.width, y: 0)
  transform = transform.scaledBy(x: -1, y: 1)
}

if ( self.imageOrientation == UIImage.Orientation.leftMirrored || self.imageOrientation == UIImage.Orientation.rightMirrored ) {
  transform = transform.translatedBy(x: self.size.height, y: 0);
  transform = transform.scaledBy(x: -1, y: 1);
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let ctx: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
                               bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
                               space: self.cgImage!.colorSpace!,
                               bitmapInfo: self.cgImage!.bitmapInfo.rawValue)!;

ctx.concatenate(transform)

if ( self.imageOrientation == UIImage.Orientation.left ||
     self.imageOrientation == UIImage.Orientation.leftMirrored ||
     self.imageOrientation == UIImage.Orientation.right ||
     self.imageOrientation == UIImage.Orientation.rightMirrored ) {
  ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
} else {
  ctx.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
}

// And now we just create a new UIImage from the drawing context and return it
return UIImage(cgImage: ctx.makeImage()!)}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接