【问题标题】:iOS: When applying a CIFilter to a UIImage the result image is rotatediOS:将 CIFilter 应用于 UIImage 时,结果图像会旋转
【发布时间】:2018-01-29 08:20:32
【问题描述】:

当我将 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

}

【问题讨论】:

标签: ios swift xcode core-image


【解决方案1】:

那是因为您的前置摄像头会像镜子一样捕捉翻转的图像。您需要做的是在捕获图像后修复图像方向。

斯威夫特 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 应用于该图像

【讨论】:

    【解决方案2】:

    确保应用滤镜之前的图像尺寸意味着宽度应小于高度,因为从前置摄像头拍摄的图像/照片的宽度大于长度,这可能会在应用滤镜后导致问题。 https://developer.apple.com/library/content/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.html

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多