gpt4 book ai didi

ios - 旋转 UIImage 的问题 - IOS/Swift

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:42 25 4
gpt4 key购买 nike

为了旋转 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;
}
}

这就是我调用扩展方法并将返回的图像设置到 ImageView 的方式。 ImageView 的“contentMode”属性设置为“缩放以填充”

currentPagePreviewImageVew.image = currentPagePreviewImageVew.image!.imageRotatedByDegrees(90, flip: false);

这是按下旋转按钮之前的样子

enter image description here

这是在按下旋转按钮一次之后。(看到图像没有旋转但是它的大小已经改变)

enter image description here

这是图像旋转 90 度时的情况。(这里是在图像旋转之后,但如上图所示,当图像处于 0 度和 180 度时其他 View 会被拉伸(stretch) (2))

enter image description here

谁能告诉我我的代码有什么问题,如果有更好的解决方案请告诉我。任何帮助将不胜感激。

已编辑: 拉伸(stretch) View 是因为约​​束问题。我通过对 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
}

关于ios - 旋转 UIImage 的问题 - IOS/Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com