2016-07-08 35 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

當然,我還有一個問題可以我問它 –