[英]Issue in rotating UIImage - IOS/Swift
為了旋轉UIImage,我使用了從互聯網上下載的擴展程序。 在那里,旋轉工作但有一些有線行為。
這是擴展代碼
import UIKit
extension UIImage {
public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return $0 * (180.0 / CGFloat(M_PI));
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / 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;
}
}
這就是我調用擴展方法並將返回的圖像設置為圖像視圖的方法。 圖像視圖的'contentMode'屬性設置為'Scale to Fill'
currentPagePreviewImageVew.image = currentPagePreviewImageVew.image!.imageRotatedByDegrees(90, flip: false);
這是按下旋轉按鈕之前的樣子
這是在按下旋轉按鈕一次之后(參見圖像未旋轉但尺寸已更改)
這是當圖像旋轉90度時(圖像旋轉后,如圖所示,當圖像處於0和180度時,其他視圖會被拉伸,如上面的屏幕(2))
有人可以告訴我我的代碼有什么問題,如果有更好的解決方案,請告訴我。 任何幫助將受到高度贊賞。
編輯:拉伸視圖是因為存在約束問題。 我通過在imageview上方的工具欄上設置一個高度約束來解決它。 但仍然無法找到第一次的答案。
func flipImageVertical(originalImage: UIImage) -> UIImage
{
let tempImageView:UIImageView = UIImageView(image: originalImage)
UIGraphicsBeginImageContext(tempImageView.frame.size)
let context:CGContextRef = UIGraphicsGetCurrentContext()!
let flipVertical:CGAffineTransform = CGAffineTransformMake(1,0,0,-1,0,tempImageView.frame.size.height)
CGContextConcatCTM(context, flipVertical)
tempImageView.layer .renderInContext(context)
let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return flippedImage
}
func flipImageHorizontal(originalImage: UIImage) -> UIImage
{
let tempImageView:UIImageView = UIImageView(image: originalImage)
UIGraphicsBeginImageContext(tempImageView.frame.size)
let context:CGContextRef = UIGraphicsGetCurrentContext()!
let flipHorizontal:CGAffineTransform = CGAffineTransformMake(-1,0,0,1,tempImageView.frame.size.width,0)
CGContextConcatCTM(context, flipHorizontal)
tempImageView.layer .renderInContext(context)
let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return flippedImage
}
你必須試試這個:
extension UIImage {
public func imageRotatedByDegrees(degrees: Double, flip: Bool) -> UIImage {
// 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(CGFloat(toRadian(degrees)))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
CGContextSetAllowsAntialiasing(bitmap, true)
CGContextSetShouldAntialias(bitmap, true)
CGContextSetInterpolationQuality(bitmap, CGInterpolationQuality.High)
// 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, CGFloat(toRadian(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
}
然后使用此方法來滿足您的新大小:
extension UIImage {
public func resizeImage(newSize:CGSize) -> UIImage {
let newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height))
let imageRef:CGImageRef = self.CGImage!
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
let context:CGContextRef = UIGraphicsGetCurrentContext()!
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)
CGContextSetAllowsAntialiasing(context, true)
CGContextSetShouldAntialias(context, true)
let flipVertical:CGAffineTransform = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height)
CGContextConcatCTM(context, flipVertical)
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef)
// Get the resized image from the context and a UIImage
let newImage:UIImage = UIImage(CGImage: CGBitmapContextCreateImage(context)!)
UIGraphicsEndImageContext();
return newImage
}
}
我使用下面的擴展,它工作正常。 實際上這很快 。
import UIKit
extension UIImage {
public func rotateImageByDegrees(_ degrees: CGFloat) -> UIImage {
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / 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: CGPoint.zero, size: self.size))
let t = CGAffineTransform(rotationAngle: 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.
bitmap?.translateBy(x: rotatedSize.width / 2.0, y: rotatedSize.height / 2.0);
// Rotate the image context
bitmap?.rotate(by: degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
bitmap?.scaleBy(x: 1.0, y: -1.0)
bitmap?.draw(self.cgImage!, in: CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width: self.size.width, height: self.size.height))
let cgimage:CGImage = bitmap!.makeImage()!
return UIImage(cgImage: cgimage)
}
}
所有其他事情都與問題中的相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.