Ios 如何为GLKView显示CIImage实现contentMode?

Ios 如何为GLKView显示CIImage实现contentMode?,ios,ciimage,glkview,contentmode,Ios,Ciimage,Glkview,Contentmode,我制作了一个GLKView子类来显示CIImage,以便更好地显示CIFilter链的输出图像 但是,似乎我需要实现contentMode逻辑,以便为drawImage(image:CIImage,inRect:CGRect,fromRect:CGRect)调用indrawRect(rect:CGRect)调用正确的inRect 任何人都知道如何实现这样的逻辑,以便与iOS 10.3中UIImageView `?的contentMode行为保持一致。contentMode的设置似乎不会影响glk

我制作了一个
GLKView
子类来显示
CIImage
,以便更好地显示
CIFilter
链的输出图像

但是,似乎我需要实现
contentMode
逻辑,以便为
drawImage(image:CIImage,inRect:CGRect,fromRect:CGRect)调用
in
drawRect(rect:CGRect)
调用正确的
inRect


任何人都知道如何实现这样的逻辑,以便与iOS 10.3中UIImageView `?

contentMode行为保持一致。contentMode的设置似乎不会影响glkView。
它看起来总是一个左下角模式

要使glkView以可查看的格式显示CIImage,这对我很有用

在glkView(view:glkView,drawIn rect:CGRect)中传递的rect需要由视图contentScaleFactor修改

  • 此代码假定您已在GLKViewController的viewDidLoad方法中初始化了EAGLContext(我的glkView名为effectView2)

  • 设置glkView后设置比例因子

    myScaleFactor=view.contentScaleFactor

  • 在glkView(view:glkView,drawIn rect:CGRect)中执行此操作

    设adjustedRect=CGRect(x:0.0,y:0.0,宽度:rect.width*myScaleFactor,高度:rect.height*myScaleFactor)

  • 然后使用ciContext在adjustedRect中绘制ciImage。将ciImage的范围用作from parm,如下所示:

    ciContext!.draw(outputImage, in: adjustedRect , from: (ciSourceImage?.extent)!)
    
  • 哦,是的。我想您已经找到了调整GLKView比例因子的技术说明。它在GLKView的子类中调用一行。看
  • 在这种方法中,您很可能需要一些调整。我目前正在为不同来源的图像进行问题导向的更改

    我花了很长时间才找到这么多,所以我想分享一下这些发现。。 请张贴您发现的任何改进

    希望能有帮助
    将L-B设置为使contentMode以与UIView类似的方式工作。我上了下面的课

    final class ContenModeEnforcer {
        static func rectFor(contentMode: UIViewContentMode, fromRect: CGRect, toRect: CGRect) -> CGRect {
            switch contentMode {
            case .scaleToFill:
                return toRect
    
            case .scaleAspectFit:
                return aspectFit(fromRect, toRect: toRect)
    
            case .scaleAspectFill:
                return aspectFill(fromRect, toRect: toRect)
    
            case .redraw:
                return fromRect
    
            case .center:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .top:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottom:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .left:
                let origin = CGPoint(
                    x: 0.0,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .right:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
    
            case .topLeft:
                let origin = CGPoint(
                    x: 0.0,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .topRight:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottomLeft:
                let origin = CGPoint(
                    x: 0.0,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottomRight:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
            }
        }
    
        static fileprivate func aspectFit(_ fromRect: CGRect, toRect: CGRect) -> CGRect {
            let fromAspectRatio = fromRect.size.width / fromRect.size.height;
            let toAspectRatio = toRect.size.width / toRect.size.height;
    
            var fitRect = toRect
    
            if (fromAspectRatio > toAspectRatio) {
                fitRect.size.height = toRect.size.width / fromAspectRatio;
                fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
            } else {
                fitRect.size.width = toRect.size.height  * fromAspectRatio;
                fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
            }
    
            return fitRect.integral
        }
    
        static fileprivate func aspectFill(_ fromRect: CGRect, toRect: CGRect) -> CGRect {
            let fromAspectRatio = fromRect.size.width / fromRect.size.height;
            let toAspectRatio = toRect.size.width / toRect.size.height;
    
            var fitRect = toRect
    
            if (fromAspectRatio > toAspectRatio) {
                fitRect.size.width = toRect.size.height  * fromAspectRatio;
                fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
            } else {
                fitRect.size.height = toRect.size.width / fromAspectRatio;
                fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
            }
    
            return fitRect.integral
        }
    }
    
    在绘图中(u:CGRect)


    你弄明白了吗?我找到了这个回购协议:。但对于左、右、下等,其行为与UIImageView不同
    final class ContenModeEnforcer {
        static func rectFor(contentMode: UIViewContentMode, fromRect: CGRect, toRect: CGRect) -> CGRect {
            switch contentMode {
            case .scaleToFill:
                return toRect
    
            case .scaleAspectFit:
                return aspectFit(fromRect, toRect: toRect)
    
            case .scaleAspectFill:
                return aspectFill(fromRect, toRect: toRect)
    
            case .redraw:
                return fromRect
    
            case .center:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .top:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottom:
                let origin = CGPoint(
                    x: (toRect.size.width - fromRect.size.width) / 2,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .left:
                let origin = CGPoint(
                    x: 0.0,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .right:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: (toRect.size.height - fromRect.size.height) / 2)
                return CGRect(origin: origin, size: fromRect.size)
    
    
            case .topLeft:
                let origin = CGPoint(
                    x: 0.0,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .topRight:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: 0.0)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottomLeft:
                let origin = CGPoint(
                    x: 0.0,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
    
            case .bottomRight:
                let origin = CGPoint(
                    x: toRect.size.width - fromRect.size.width,
                    y: toRect.size.height - fromRect.size.height)
                return CGRect(origin: origin, size: fromRect.size)
            }
        }
    
        static fileprivate func aspectFit(_ fromRect: CGRect, toRect: CGRect) -> CGRect {
            let fromAspectRatio = fromRect.size.width / fromRect.size.height;
            let toAspectRatio = toRect.size.width / toRect.size.height;
    
            var fitRect = toRect
    
            if (fromAspectRatio > toAspectRatio) {
                fitRect.size.height = toRect.size.width / fromAspectRatio;
                fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
            } else {
                fitRect.size.width = toRect.size.height  * fromAspectRatio;
                fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
            }
    
            return fitRect.integral
        }
    
        static fileprivate func aspectFill(_ fromRect: CGRect, toRect: CGRect) -> CGRect {
            let fromAspectRatio = fromRect.size.width / fromRect.size.height;
            let toAspectRatio = toRect.size.width / toRect.size.height;
    
            var fitRect = toRect
    
            if (fromAspectRatio > toAspectRatio) {
                fitRect.size.width = toRect.size.height  * fromAspectRatio;
                fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
            } else {
                fitRect.size.height = toRect.size.width / fromAspectRatio;
                fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
            }
    
            return fitRect.integral
        }
    }
    
    let inputBounds = image.extent
    let drawableBounds = CGRect(x: 0, y: 0, width: self.drawableWidth, height: self.drawableHeight)
    let targetBounds = ContenModeEnforcer.rectFor(contentMode: contentMode, fromRect: inputBounds, toRect: drawableBounds)
    ciContext.draw(image, in: targetBounds, from: inputBounds)