首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CIFilter自动旋转图像

CIFilter自动旋转图像
EN

Stack Overflow用户
提问于 2015-03-12 07:06:15
回答 1查看 578关注 0票数 0

使用后,CIFilter会自动将图像旋转90度。

是否有类似于objective-c的快速修复方法:

Image auto-rotates after using CIFilter

这是这个问题的正确解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2016-02-10 04:52:12

我也遇到了同样的问题,并且很难为swift找到资源。我最终合并了两个示例,并使用了以下代码:

代码语言:javascript
复制
var angle = 0.0;
if (originImage.imageOrientation == UIImageOrientation.Right)
{
    angle = 90.0
}
else if (originImage.imageOrientation == UIImageOrientation.Left)
{
    angle = -90.0
}
else if (originImage.imageOrientation == UIImageOrientation.Down)
{
    angle = 180
}
else if (originImage.imageOrientation == UIImageOrientation.Up)
{
    angle = 0.0
}
filteredImage = filteredImage.imageRotatedByDegrees(CGFloat(angle), flip: false)

使用此扩展名:

代码语言:javascript
复制
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
}
}

我猜这是可以简化的,但它对我来说是有效的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28999069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档