iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了旋转UIImage的问题 – IOS / Swift大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
为了旋转UI Image,我使用了从互联网上下载的扩展程序.在那里,旋转工作但有一些有线行为.

这是扩展代码

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 co@R_675_10443@t
        UIGraphicsBeginImageCo@R_675_10443@t(rotatedSizE);
        let bitmap = UIGraphicsGetCurrentCo@R_675_10443@t();

        // Move the origin to the middle of the image so we will rotate and scale around the center.
        CGCo@R_675_10443@tTranslateCTM(bitmap,rotatedSize.width / 2.0,rotatedSize.height / 2.0);

        // Rotate the image co@R_675_10443@t
        CGCo@R_675_10443@tRotateCTM(bitmap,degreesToradians(degrees));

        // Now,draw the rotated/scaled imagE into the co@R_675_10443@t
        var yFlip: CGFloat;

        if(flip){
            yFlip = CGFloat(-1.0);
        } else {
            yFlip = CGFloat(1.0);
        }

        CGCo@R_675_10443@tScaleCTM(bitmap,yFlip,-1.0);
        CGCo@R_675_10443@tDrawImage(bitmap,CGRectMake(-size.width / 2,-size.height / 2,size.width,size.height),CGImagE);

        let newImage = UIGraphicsGetImageFromCurrentImageCo@R_675_10443@t();
        UIGraphicsEndImageCo@R_675_10443@t();

        return newImage;
    }
}

这就是我调用扩展方法并将返回的图像设置为图像视图的方法.图像视图的’contentMode’属性设置为’Scale to Fill’

currentPagePreviewImageVew.image = currentPagePreviewImageVew.image!.imageRotatedBydegrees(90,flip: falsE);

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

旋转UIImage的问题 – IOS / Swift

这是在按下旋转按钮一次之后(参见图像未旋转但尺寸已更改)

旋转UIImage的问题 – IOS / Swift

这是当图像旋转90度时(图像旋转后,如图所示,当图像处于0和180度时,其他视图会被拉伸,如上面的屏幕(2))

旋转UIImage的问题 – IOS / Swift

有人可以告诉我我的代码有什么问题,如果有更好的解决方案,请告诉我.任何帮助将受到高度赞赏.

编辑:拉伸视图是因为存在约束问题.我通过在imageview上方的工具栏上设置一个高度约束来解决它.但仍然无法找到第一次的答案.

解决方法

func flipImageVertical(originalImage: UIImagE) -> UIImage
    {
        let tempImageView:UIImageView = UIImageView(image: originalImagE)
        UIGraphicsBeginImageCo@R_675_10443@t(tempImageView.frame.sizE)
        let co@R_675_10443@t:CGCo@R_675_10443@tRef = UIGraphicsGetCurrentCo@R_675_10443@t()!
        let flipVertical:CGAffineTransform = CGAffineTransformMake(1,-1,tempImageView.frame.size.height)
        CGCo@R_675_10443@tConcatCTM(co@R_675_10443@t,flipVertical)
        tempImageView.layer .renderInCo@R_675_10443@t(co@R_675_10443@t)

        let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageCo@R_675_10443@t()
        UIGraphicsEndImageCo@R_675_10443@t()
        return flippedImage
    }

    func flipImageHorizontal(originalImage: UIImagE) -> UIImage
    {
        let tempImageView:UIImageView = UIImageView(image: originalImagE)
        UIGraphicsBeginImageCo@R_675_10443@t(tempImageView.frame.sizE)
        let co@R_675_10443@t:CGCo@R_675_10443@tRef = UIGraphicsGetCurrentCo@R_675_10443@t()!
        let flipHorizontal:CGAffineTransform = CGAffineTransformMake(-1,1,tempImageView.frame.size.width,0)

        CGCo@R_675_10443@tConcatCTM(co@R_675_10443@t,flipHorizontal)
        tempImageView.layer .renderInCo@R_675_10443@t(co@R_675_10443@t)

        let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageCo@R_675_10443@t()
        UIGraphicsEndImageCo@R_675_10443@t()
        return flippedImage
    }

大佬总结

以上是大佬教程为你收集整理的旋转UIImage的问题 – IOS / Swift全部内容,希望文章能够帮你解决旋转UIImage的问题 – IOS / Swift所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。