首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在iOS中显示钻石形状的图像?

如何在iOS中显示钻石形状的图像?
EN

Stack Overflow用户
提问于 2016-07-08 07:17:35
回答 2查看 782关注 0票数 2

原来的图像是这样的海,我是iOS的新手,我的要求是显示钻石形状的图像,为此,我遵循以下代码。我拿了一个UIView,ImageView是那个UIView的子视图。我得到的钻石形状,但形象是翻转。如何解决这个问题?

代码语言:javascript
复制
  var tr: CGAffineTransform = CGAffineTransformIdentity
  tr = CGAffineTransformScale(tr, 0.8, 1)
  tr = CGAffineTransformRotate(tr, 0.7)
  self.views.transform =  tr
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-08 07:25:23

您可以使用UIBezierPath绘制形状

代码语言:javascript
复制
import UIKit

extension UIView
{
    func addDiamondMaskToView()
    {
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: self.bounds.size.width / 2.0, y: 0))
    path.addLineToPoint(CGPoint(x: self.bounds.size.width, y: self.bounds.size.height / 2.0))
    path.addLineToPoint(CGPoint(x: self.bounds.size.width / 2.0, y: self.bounds.size.height))
    path.addLineToPoint(CGPoint(x: 0, y: self.bounds.size.height / 2.0))
    path.closePath()

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.CGPath
    shapeLayer.fillColor = UIColor.whiteColor().CGColor
    shapeLayer.strokeColor = UIColor.clearColor().CGColor

    self.layer.mask = shapeLayer
    }
}

您可以将该方法调用为

代码语言:javascript
复制
//suppose this is your imageview
let imgView = UIImageView(frame: CGRectMake(0, 0, 100, 200))
//then call the method as
imgView.addDiamondMaskToView()

它对我来说很好,检查这些图像以供参考。

票数 2
EN

Stack Overflow用户

发布于 2016-07-08 14:21:11

由于您提供了图像,在分析之后,我可以得出结论,您需要做的是

  • 向ImageView添加对角线掩码
  • 将imageView内的图像旋转到一定的角度

假设图像旋转到45度,我提供的解决方案如下

代码语言:javascript
复制
import UIKit

extension UIView
{
func addDiamondMaskToView()
{
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: self.bounds.size.width / 2.0, y: 0))
    path.addLineToPoint(CGPoint(x: self.bounds.size.width, y: self.bounds.size.height / 2.0))
    path.addLineToPoint(CGPoint(x: self.bounds.size.width / 2.0, y: self.bounds.size.height))
    path.addLineToPoint(CGPoint(x: 0, y: self.bounds.size.height / 2.0))
    path.closePath()

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.CGPath
    shapeLayer.fillColor = UIColor.whiteColor().CGColor
    shapeLayer.strokeColor = UIColor.clearColor().CGColor
    self.layer.mask = shapeLayer
}
}

extension UIImageView
{
    func addDiamondWithSomeAngle(angleInDegrees degrees : CGFloat)
    {
        self.addDiamondMaskToView()
        self.image = self.image?.imageRotatedByDegrees(degrees, flip: false)
    }
}

extension UIImage {
    public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage 
    {
        let degreesToRadians: (CGFloat) -> CGFloat = {
            return $0 / 180.0 * CGFloat(M_PI)
        }
        let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
        let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
        rotatedViewBox.transform = t
        let rotatedSize = rotatedViewBox.frame.size

        UIGraphicsBeginImageContext(rotatedSize)
        let bitmap = UIGraphicsGetCurrentContext()

        CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);

        CGContextRotateCTM(bitmap, degreesToRadians(degrees));

        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
    }
}

您需要将该方法调用为

代码语言:javascript
复制
imageView.addDiamondWithSomeAngle(angleInDegrees: 45)

输出如下

提供:图像旋转confile

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

https://stackoverflow.com/questions/38260945

复制
相关文章

相似问题

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