计算旋转后图片大小

demo地址 https://github.com/nullLuli/ImageSizeAfterTransformDemo

方法一:计算图片每个角旋转后坐标,然后计算外围矩形 (原理型)

    class func caculateFrame(frame: CGRect, after trans: CGAffineTransform) -> CGRect {
        //找出四个frame的四个角坐标
        let point1 = CGPoint(x: frame.minX, y: frame.minY)
        let point2 = CGPoint(x: frame.maxX, y: frame.minY)
        let point3 = CGPoint(x: frame.maxX, y: frame.maxY)
        let point4 = CGPoint(x: frame.minX, y: frame.maxY)
        
        //计算出四个角以center为坐标系原点的相对坐标
        let point1RP = CGPoint(x: point1.x - frame.midX, y: point1.y - frame.midY)
        let point2RP = CGPoint(x: point2.x - frame.midX, y: point2.y - frame.midY)
        let point3RP = CGPoint(x: point3.x - frame.midX, y: point3.y - frame.midY)
        let point4RP = CGPoint(x: point4.x - frame.midX, y: point4.y - frame.midY)
        
        //旋转相对坐标
        let point1RPAfter = __CGPointApplyAffineTransform(point1RP, trans)
        let point2RPAfter = __CGPointApplyAffineTransform(point2RP, trans)
        let point3RPAfter = __CGPointApplyAffineTransform(point3RP, trans)
        let point4RPAfter = __CGPointApplyAffineTransform(point4RP, trans)
        
        //从相对坐标转换成iOS系统的坐标
        let point1After = CGPoint(x: point1RPAfter.x + frame.midX, y: point1RPAfter.y + frame.midY)
        let point2After = CGPoint(x: point2RPAfter.x + frame.midX, y: point2RPAfter.y + frame.midY)
        let point3After = CGPoint(x: point3RPAfter.x + frame.midX, y: point3RPAfter.y + frame.midY)
        let point4After = CGPoint(x: point4RPAfter.x + frame.midX, y: point4RPAfter.y + frame.midY)
        
        //遍历找出四个新角
        var maxX: CGFloat = point4After.x //随便给个初始值,后面会遍历
        var maxY: CGFloat = point4After.y
        var minX: CGFloat = point1After.x
        var minY: CGFloat = point1After.y
        for point in [point1After, point2After, point3After, point4After] {
            if point.x > maxX {
                maxX = point.x
            }
            
            if point.x < minX {
                minX = point.x
            }
            
            if point.y > maxY {
                maxY = point.y
            }
            
            if point.y < minY {
                minY = point.y
            }
        }
        
        return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)
    }

方法二:使用UIView的transform让系统帮我们计算

    class func caculateFrame2(frame: CGRect, after trans: CGAffineTransform) -> CGRect {
        let rotatedImageBox = UIView(frame: frame)
        rotatedImageBox.frame = frame
        rotatedImageBox.transform = trans
        return rotatedImageBox.frame
    }

方法三:使用CGRect.applying方法计算

    class func caculateFrame3(frame: CGRect, after trans: CGAffineTransform) -> CGRect {
        let frameR = CGRect(origin: CGPoint(x: -frame.width / 2, y: -frame.height / 2), size: frame.size)
        let frameAfterR = frameR.applying(trans)
        let frameAfter = CGRect(origin: CGPoint(x: frameAfterR.origin.x + frame.midX, y: frameAfterR.origin.y + frame.midY), size: frameAfterR.size)
        return frameAfter
    }

下面是原文


方法一:计算图片每个角旋转后坐标,然后计算外围矩形
假设图片位置(x,y,width,height)
则图片四个角坐标分别是
point1 = (x,y);
point2 = (x+width, y);
point3 = (x+width, y+height);
point4 =(x,y+height);
旋转中心点为(centerX,centerY)
旋转变换矩阵为trans(iOS中CGAffineTransform,安卓中的Matrix)
旋转后每个角的坐标分别是
point1 = (CGPointApplyAffineTransform(x-centerX, trans) + centerX,CGPointApplyAffineTransform(y-centerY, trans) + centerY);
point2 = (CGPointApplyAffineTransform(x+width-centerX, trans) + centerX,CGPointApplyAffineTransform(y-centerY, trans) + centerY);
point3 = (CGPointApplyAffineTransform(x+width-centerX, trans) + centerX,CGPointApplyAffineTransform(y+height-centerY, trans) + centerY);
point4 = (CGPointApplyAffineTransform(x-centerX, trans) + centerX,CGPointApplyAffineTransform(y+height-centerY, trans) + centerY);
遍历旋转后的点找出x、y方向的最大、最小值
将这些最大、最小值组合成为新的矩形,这个矩形就是我们要找的旋转后的图片大小

方法二:
UIView * rotatedImageBox = [[UIView alloc]initWithFrame:imageFrame];//imageFrame是旋转前的图片位置
CGFloat rotateAnchorX = (-imageFrame.origin.x + rotateCenter.x)/imageFrame.size.width;
CGFloat rotateAnchorY = (-imageFrame.origin.y + rotateCenter.y)/imageFrame.size.height;
rotatedImageBox.layer.anchorPoint = CGPointMake(rotateAnchorX, rotateAnchorY);
rotatedImageBox.frame = imageFrame;
rotatedImageBox.transform = trans;
CGRect timeFrameInPDF = rotatedImageBox.frame;

你可能感兴趣的:(计算旋转后图片大小)