UIImage 3 变换 swift
UIImage 3Transform swift
我怎样才能将图像转换为看起来像图像中的图像?
只是想了解这种转变是如何进行的
用于使用 UIImage 的扩展来旋转 im,但目前仅此而已,只是 swift
中的新内容
public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return [=11=] * (180.0 / CGFloat(M_PI))
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return [=11=] / 180.0 * CGFloat(M_PI)
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);
// // Rotate the image context
CGContextRotateCTM(bitmap, degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
CGContextScaleCTM(bitmap, yFlip, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
您需要将 Core Animation 与 CATransform3D 结合使用,并调整变换以提供深度印象。您所做的是对变换矩阵的 M34 分量应用一个小的负值(-1/200 是一个很好的起点)。您可以对每个视图的图层执行此操作,或者在超级图层上使用 CATransformLayer,然后将其他图像添加为变换图层的子图层。
这是棘手、挑剔的事情。我已经有一段时间没有弄乱它了,所以我不再记得细节了,但这应该足以让你开始了。我建议搜索 "iOS perspective"、"M34" 和 "CATransformLayer" 以了解更多信息。
我怎样才能将图像转换为看起来像图像中的图像? 只是想了解这种转变是如何进行的 用于使用 UIImage 的扩展来旋转 im,但目前仅此而已,只是 swift
中的新内容public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return [=11=] * (180.0 / CGFloat(M_PI))
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return [=11=] / 180.0 * CGFloat(M_PI)
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);
// // Rotate the image context
CGContextRotateCTM(bitmap, degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
CGContextScaleCTM(bitmap, yFlip, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
您需要将 Core Animation 与 CATransform3D 结合使用,并调整变换以提供深度印象。您所做的是对变换矩阵的 M34 分量应用一个小的负值(-1/200 是一个很好的起点)。您可以对每个视图的图层执行此操作,或者在超级图层上使用 CATransformLayer,然后将其他图像添加为变换图层的子图层。
这是棘手、挑剔的事情。我已经有一段时间没有弄乱它了,所以我不再记得细节了,但这应该足以让你开始了。我建议搜索 "iOS perspective"、"M34" 和 "CATransformLayer" 以了解更多信息。