demo地址 https://github.com/nullLuli/ImageSizeAfterTransformDemo
方法一:計(jì)算圖片每個(gè)角旋轉(zhuǎn)后坐標(biāo),然后計(jì)算外圍矩形 (原理型)
class func caculateFrame(frame: CGRect, after trans: CGAffineTransform) -> CGRect {
//找出四個(gè)frame的四個(gè)角坐標(biāo)
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)
//計(jì)算出四個(gè)角以center為坐標(biāo)系原點(diǎn)的相對(duì)坐標(biāo)
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)
//旋轉(zhuǎn)相對(duì)坐標(biāo)
let point1RPAfter = __CGPointApplyAffineTransform(point1RP, trans)
let point2RPAfter = __CGPointApplyAffineTransform(point2RP, trans)
let point3RPAfter = __CGPointApplyAffineTransform(point3RP, trans)
let point4RPAfter = __CGPointApplyAffineTransform(point4RP, trans)
//從相對(duì)坐標(biāo)轉(zhuǎn)換成iOS系統(tǒng)的坐標(biāo)
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)
//遍歷找出四個(gè)新角
var maxX: CGFloat = point4After.x //隨便給個(gè)初始值崭倘,后面會(huì)遍歷
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讓系統(tǒng)幫我們計(jì)算
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方法計(jì)算
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
}
下面是原文
方法一:計(jì)算圖片每個(gè)角旋轉(zhuǎn)后坐標(biāo)馏谨,然后計(jì)算外圍矩形
假設(shè)圖片位置(x,y,width,height)
則圖片四個(gè)角坐標(biāo)分別是
point1 = (x,y);
point2 = (x+width, y);
point3 = (x+width, y+height);
point4 =(x,y+height);
旋轉(zhuǎn)中心點(diǎn)為(centerX,centerY)
旋轉(zhuǎn)變換矩陣為trans(iOS中CGAffineTransform鸟蟹,安卓中的Matrix)
旋轉(zhuǎn)后每個(gè)角的坐標(biāo)分別是
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);
遍歷旋轉(zhuǎn)后的點(diǎn)找出x、y方向的最大、最小值
將這些最大乱投、最小值組合成為新的矩形送爸,這個(gè)矩形就是我們要找的旋轉(zhuǎn)后的圖片大小
方法二:
UIView * rotatedImageBox = [[UIView alloc]initWithFrame:imageFrame];//imageFrame是旋轉(zhuǎn)前的圖片位置
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;