1

When I try to use UIGraphicsImageRenderer in a watchOS app, the compiler throws an error. The entry in the documentation looks like it is only available in iOS and tvOS. Do you know why that would be the case?

import WatchKit

struct ImageGenerator() {
  func image() -> UIImage {
    let format = UIGraphicsImageRendererFormat() // ERROR
    format.scale = 1
    format.opaque = true

    let renderer = UIGraphicsImageRenderer(size: size, format: format) // ERROR
    let image = renderer.image { imageRendererContext in
      // ...
    }
  }
}
Ben Morrow
  • 832
  • 6
  • 17

1 Answers1

3

UIGraphicsImageRenderer is not available on watchOS. However, you can still use legacy render API on watchOS:

func image() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(yourImageSize, isOpaque, scale)
    defer { UIGraphicsEndImageContext() }
    let context = UIGraphicsGetCurrentContext()! 

    // draw your image at here...

    return UIGraphicsGetImageFromCurrentImageContext()! // get image
}
Jonny
  • 1,969
  • 18
  • 25