ฉันต้องการสร้าง UII โดยใช้โปรแกรมแบบเต็มไปด้วยสีทึบ ใครมีความคิดว่าจะทำสิ่งนี้ใน Swift ได้อย่างไร
ฉันต้องการสร้าง UII โดยใช้โปรแกรมแบบเต็มไปด้วยสีทึบ ใครมีความคิดว่าจะทำสิ่งนี้ใน Swift ได้อย่างไร
คำตอบ:
อีกวิธีที่ดีที่เข้ากันได้กับSwift 2.2คือการสร้างนวกรรมิกตัวใหม่ใน UIImage ด้วยวิธีนี้:
public extension UIImage {
    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        guard let cgImage = image?.CGImage else { return nil }
        self.init(CGImage: cgImage)
    }  
}
ด้วยวิธีนี้คุณสามารถสร้างภาพสีที่กำหนดเองด้วยวิธีนี้:
let redImage = UIImage(color: .redColor())
หรืออาจสร้างรูปภาพด้วยขนาดที่กำหนดเอง:
let redImage200x200 = UIImage(color: .redColor(), size: CGSize(width: 200, height: 200))
Swift 3.0
public extension UIImage {
  public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
    let rect = CGRect(origin: .zero, size: size)
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
    color.setFill()
    UIRectFill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    guard let cgImage = image?.cgImage else { return nil }
    self.init(cgImage: cgImage)
  }
}
              UIGraphicsBeginImageContextWithOptions(rect.size, true, 1.0)  อ้างอิง: developer.apple.com/library/ios/documentation/UIKit/Reference/…
                    image? ทำไมต้องทำเช่นนี้? self.init(cgImage: cgImage)
                    UIImageในตัวสร้างได้ แต่คุณสามารถโยงผ่านตัวUIImage.init(cgImage: )สร้างได้
                    self.init(cgImage: cgImage, scale: image!.scale, orientation: image!.imageOrientation)
                    นี่คือตัวเลือกอื่น ฉันเชื่อว่าคุณต้องการวัตถุ UIImage ที่แน่นอน
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRectMake(0, 0, size.width, size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}
ติดนี้ในรหัส Swift ของคุณและเรียกมันว่า
สวิฟท์ 3.1:
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}
              UIRectMake(0, 0, size.width, size.height)นี่น่าจะเป็นวิธีที่ถูกต้อง: CGRectMake(0, 0, size.width, size.height).
                    letแทนvarตัวแปรที่ไม่เปลี่ยนรูป ที่นี่ดูเหมือนว่าไม่ควรrectและไม่imageควรใช้varการประกาศ
                    รุ่น Swift 4:
extension UIColor {
    func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero, size: size))
        }
    }
}
การใช้งาน:
let image0 = UIColor.orange.image(CGSize(width: 128, height: 128))
let image1 = UIColor.yellow.image()
              วิธีการที่สะอาดกว่านั้นก็คือการห่อหุ้มตรรกะภายในUIImageส่วนขยาย:
import UIKit
extension UIImage {
  class func imageWithColor(color: UIColor) -> UIImage {
    let rect: CGRect = CGRectMake(0, 0, 1, 1)
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
  }
}
ตอนนี้ผู้บริโภคสามารถโทรUIImage.imageWithColor(UIColor.blackColor())เพื่อสร้างภาพที่มีพื้นหลังสีดำ
class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.backgroundColor = UIColor.redColor()
    }
}
วิธีการที่คล้ายกันถ้าคุณต้องการวาดภาพด้วยตัวคุณเองและเชื่อมต่อผ่าน IBOutlet
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        var frame = CGRectMake(100,100,100,100)
        var imageView2 = UIImageView(frame: frame)
        imageView2.backgroundColor = UIColor.redColor()
        self.view.addSubview(imageView2)
    }
}
วิธีที่สามที่ยืมมาจาก anthonyliao ซับซ้อนกว่าเล็กน้อย:
class ViewController: UIViewController {
    func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(CGRectMake(0, 0, 100, 100))
        var image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        var imageView = UIImageView(frame: CGRectMake(100,100,100,100))
        let screenImage = getImageWithColor(UIColor.redColor(), size: CGSize(width: 100, height: 100))
        imageView.image = screenImage
        self.view.addSubview(imageView)
    }
}
              UIImage UIImageView
                    getImageWithColor(color: UIColor, size: CGSize) -> UIImageนี้ก็เพียงพอแล้วสำหรับคำตอบเพราะฉันคิดว่าไม่ใช่ทุกคนที่จะใช้สิ่งนี้พร้อมกับ UIImageView
                    บิดเล็กน้อยกับคำตอบที่ยอดเยี่ยมของ @ neoneye ทำให้รหัสโทรไม่จำเป็นต้องสร้าง CGSize และเปลี่ยนชื่อเพื่อไม่ให้ชนกับคนอื่น:
สวิฟต์ 4
extension UIColor {
    func imageWithColor(width: Int, height: Int) -> UIImage {
        let size = CGSize(width: width, height: height)
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero, size: size))
        }
    }
}
              คุณสามารถใช้iOS 10 UIGraphicsImageRenderer ใหม่ API ใหม่
นี่คือส่วนขยายของ UIColor in Swift 3.1
extension UIColor {
func getImage(size: CGSize) -> UIImage {
    let renderer = UIGraphicsImageRenderer(size: size)
    return renderer.image(actions: { rendererContext in
        self.setFill()
        rendererContext.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
    })
}}
              วิธีที่ดีคือการมีคุณสมบัติที่คำนวณได้เช่นนี้:
extension UIColor {
    var imageRepresentation : UIImage {
      let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
      UIGraphicsBeginImageContext(rect.size)
      let context = UIGraphicsGetCurrentContext()
      context?.setFillColor(self.cgColor)
      context?.fill(rect)
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
    return image!
  }
}
การใช้งาน:
let redImage = UIColor.red.imageRepresentation
              @anthonyliao Swift 3 คำตอบที่ยอมรับได้:
class func getImageWithColor(color: UIColor, size: CGSize) -> UIImage
{
    let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: size.width, height: size.height))
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}
              รูปสี่เหลี่ยมผืนผ้าที่มีมุมมน
extension UIImage {
convenience init?(color: UIColor) {
    let rect = CGRect(x: 0, y: 0, width: 10, height: 2)
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 10, height: 2), false, 0)
    let bezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 8)
    color.setFill()
    bezierPath.fill()
    let image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    guard  let cgImage = image.cgImage else {
        return nil
    }
    self.init(cgImage: cgImage)
}
}
UIImageไม่มี-initWithSize:andColor:วิธี แต่NSImageใน OS X คุณใช้ไลบรารีหรือหมวดหมู่ที่กำหนดเองสำหรับสิ่งนี้หรือไม่