2016-07-08 34 views
2

Original image is like this海我是新来的iOS我的要求是钻石形状显示图像,所以为此我遵循以下代码。我带了一个UIView和ImageView是该UIView的子视图。我正在钻石形状,但图像翻转。如何解决这个问题?如何在iOS中以钻石形状显示图像?

check the output here

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

我已为另外一个答案,请 –

回答

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() 

它为我工作的罚款,检查图像为参考

enter image description here

+0

它不工作我已经应用上面的代码 –

+0

你能告诉我确切的宽度和高度uiimageview –

+0

更新答案等待 –

0

正如你所提供的图像,分析我可以断定,你需要做的后

  • 是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) 

输出如下

enter image description here

礼貌:Image rotation通过confile

+0

很好很好工作很好谢谢阿米特 –

+0

很好,请接受解决方案,并帮助他人也找到解决方案 –

+0

当然,我还有一个问题可以我问它 –