【问题标题】:How to display image in Diamond shape in iOS?如何在 iOS 中以菱形显示图像?
【发布时间】:2016-11-10 16:27:07
【问题描述】:

Original image is like this Hai 我是 iOS 新手,我的要求是以菱形显示图像,因此为此我遵循以下代码。我拿了一个 UIView 和 ImageView 是那个 UIView 的子视图。我得到钻石形状,但图像正在翻转。如何解决这个问题?

  var tr: CGAffineTransform = CGAffineTransformIdentity
  tr = CGAffineTransformScale(tr, 0.8, 1)
  tr = CGAffineTransformRotate(tr, 0.7)
  self.views.transform =  tr

【问题讨论】:

  • 我已经发布了另一个答案,请检查

标签: ios iphone swift uiimageview


【解决方案1】:

正如你提供的图片,经过分析我可以得出结论,你需要做的是

  • 向 ImageView 添加对角线蒙版
  • 将该 imageView 内的图像旋转到某个角度

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

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

你需要调用方法

imageView.addDiamondWithSomeAngle(angleInDegrees: 45)

输出如下

礼貌:Image rotationconfile

【讨论】:

  • 很好,请采纳解决方案,并帮助其他人也找到解决方案
  • 当然,我还有一个问题可以问
  • 是的,继续
  • :) ,我会尝试回答这个问题,这可能需要时间来实现。你先接受这个问题:)
  • 不,您需要接受这个作为正确答案,您需要在向上箭头下方打勾
【解决方案2】:

你可以使用UIBezierPath来绘制形状

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

您可以将方法称为

//suppose this is your imageview
let imgView = UIImageView(frame: CGRectMake(0, 0, 100, 200))
//then call the method as
imgView.addDiamondMaskToView()

它对我来说工作正常,请查看图片以供参考

【讨论】:

  • 你能告诉我uiimageview的确切宽度和高度吗
  • 更新答案等待
  • 感谢您的回复,等待您的积极反馈
  • 如果有帮助,请告诉我
  • 你的输出和我的要求有很大的不同
猜你喜欢
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 2014-06-27
  • 2016-12-06
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多