如何创建一个三角形UIImage

时间:2017-07-15 00:57:16

标签: ios swift

如何创建三角形UIImage?这就是我现在正在做的事情,但它根本不会产生任何图像。

extension UIImage {

    static func triangle(side: CGFloat, color: UIColor)->UIImage {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: side, height: side), false, 0)
        let ctx = UIGraphicsGetCurrentContext()!
        ctx.saveGState()

        ctx.beginPath()
        ctx.move(to: CGPoint(x: side / 2, y: 0))
        ctx.move(to: CGPoint(x: side, y: side))
        ctx.move(to: CGPoint(x: 0, y: side))
        ctx.move(to: CGPoint(x: side / 2, y: 0))
        ctx.closePath()

        ctx.setFillColor(color.cgColor)

        ctx.restoreGState()
        let img = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return img
    }
}

3 个答案:

答案 0 :(得分:2)

您的路径不包含任何行,因此无法填充任何区域。此外,你没有画出路径。

尝试这样的事情:

static func triangle(side: CGFloat, color: UIColor)->UIImage {
    UIGraphicsBeginImageContextWithOptions(CGSize(width: side, height: side), false, 0)
    let ctx = UIGraphicsGetCurrentContext()!
    ctx.saveGState()

    ctx.beginPath()
    ctx.move(to: CGPoint(x: side / 2, y: 0))
    //### Add lines
    ctx.addLine(to: CGPoint(x: side, y: side))
    ctx.addLine(to: CGPoint(x: 0, y: side))
    //ctx.addLine(to: CGPoint(x: side / 2, y: 0)) //### path is automatically closed
    ctx.closePath()

    ctx.setFillColor(color.cgColor)

    ctx.drawPath(using: .fill) //### draw the path

    ctx.restoreGState()
    let img = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return img
}

答案 1 :(得分:1)

或者,您可能会丢失Core Graphics代码,进一步简化它:

$wind_value = $xml->channel->item[0]->nhcCyclone->nhcwind;
$storm_wind = substr($wind_value, 0, -4);

if(($storm_wind >=1) && ($storm_wind <=38); { 
    $category = 'TD'; }
if($storm_wind >=39 && $storm_wind <=73); {
    $category = 'TS'; }
if($storm_wind >=74 && $storm_wind <=95); {
    $category = 1; }
if($storm_wind >=96 && $storm_wind <=110 ); {
    $category = 2; }
if($storm_wind >=111 && $storm_wind <=129 ); {
    $category = 3; }
if($storm_wind >=130 && $storm_wind <=156 ); {
    $category = 4; }
if($storm_wind >=157 && $storm_wind <=240 ); {
    $category = 5; }

答案 2 :(得分:0)

OOPer告诉你你缺乏线条,并且没有真正画出三角形。

除此之外,我建议避免使用图形上下文。 UIBezierPath使其更清晰,更容易绘制,而不必担心上下文。此外,您不需要保存和恢复上下文,因为您正在创建临时上下文,然后立即丢弃它。

这是一个完整的游乐场,用于绘制三角形(并填充和描绘图像矩形,以便更容易看到 - 您可以删除笔划,并可能使用UIColor.clear填充形状。)

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true


extension UIImage {

  static func triangle(side: CGFloat, color: UIColor)->UIImage {
    UIGraphicsBeginImageContextWithOptions(CGSize(width: side, height: side), false, 0)

    //First fill the bounds with white
    let rectPath = UIBezierPath(rect: CGRect(x:0, y: 0, width: side, height: side))
    UIColor.white.setFill() //Use clear if you want your image to only contain the triangle.
    rectPath.fill()

    //Now build the triangle path
    let path = UIBezierPath()
    path.move(to: CGPoint(x: side / 2, y: 0))
    path.addLine(to: CGPoint(x: side, y: side))
    path.addLine(to: CGPoint(x: 0, y: side))
    path.close() //Closing the path draws the final line
    color.setFill()
    path.fill()

    //Stroke the outer path in gray so you can see the bounds - remove if desired
    UIColor.gray.setStroke()
    rectPath.stroke()

    let img = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return img
  }
}

let imageView = UIImageView(frame: CGRect(x:0, y: 0, width: 200, height: 200))


PlaygroundPage.current.liveView = imageView
imageView.image = UIImage.triangle(side: 200.0, color: UIColor.yellow)
相关问题