UIImage
class UIImage : NSObject, NSSecureCoding
-
Create a
CGContext
allowing to do custom drawing, and returns the resulting image as drawn in the context.Declaration
Swift
public static func draw(size: CGSize, opaque: Bool = false, scale: CGFloat = 0.0, graphics: (CGContext) -> Void) -> UIImage
Parameters
size
Please refer to the parameters documentation for
UIGraphicsBeginImageContextWithOptions
for more info.opaque
Please refer to the parameters documentation for
UIGraphicsBeginImageContextWithOptions
for more info.scale
Please refer to the parameters documentation for
UIGraphicsBeginImageContextWithOptions
for more info.graphics
The drawing actions to execute.
-
Deprecated: Please use
withAlpha(_:)
instead.Refer to the documentation for
withAlpha(_:)
for more info.Declaration
Swift
@available(*, deprecated, renamed: "withAlpha(_:﹚") public func imageWithAlpha(_ alpha: CGFloat) -> UIImage
-
Returns the receiver with the given alpha applied to it. The rendering mode is kept the same. The resulting image will be made resizable whether it was originally or not, with
capInsets
set as the cap insets, andresizingMode
as its resizing mode.Declaration
Swift
public func withAlpha(_ alpha: CGFloat) -> UIImage
Parameters
alpha
The alpha to apply to the receiver.
-
Deprecated: Please use
withTint(_:)
instead.Refer to the documentation for
withTint(_:)
for more info.Declaration
Swift
@available(*, deprecated, renamed: "withTint(_:﹚") public func imageTintedWithColor(_ color: UIColor) -> UIImage
-
Apply the given tint to the image. The rendering mode is kept the same. This method takes the rgb values of each pixels in the image, and replace them with the color given as parameter. The alpha value of each pixels is kept the same. The resulting image will be made resizable whether it was originally or not, with
capInsets
set as the cap insets, andresizingMode
as its resizing mode.The behavior of this method is similar to using the
UIImage
with a.alwaysTemplate
rendering mode and using atintColor
when displaying theUIImage
in anUIImageView
.See also
Declaration
Swift
public func withTint(_ tint: UIColor) -> UIImage
Parameters
tint
The tint to apply to the receiver.
-
Apply the given color to the image. The rendering mode is kept the same. This method keeps the brightness of all pixels the same, but updates the saturation and hue to that of the given color.
In other words, this methods can be used to color grayscale images or tint images without loosing contrast information.
See also
Declaration
Swift
public func withColor(_ color: UIColor) -> UIImage
Parameters
color
The color to apply to the receiver.
-
Apply the given tint to the image. The rendering mode is kept the same.
Declaration
Swift
public func resized(offset: CGPoint = .zero, contextSize: CGSize? = nil, size: CGSize) -> UIImage?
Parameters
offset
The offset to apply to the original image in the resulting image.
contextSize
The size of the context used to resize the image. If not specified, it defaults to
CGSize(width: offset.x + size.width, height: offset.y + size.height)
size
The size to resize the image to.
-
Deprecated: Please use
ofColor(_:)
instead.Refer to the documentation for
ofColor(_:)
for more info.Declaration
Swift
@available(*, deprecated, renamed: "ofColor(_:﹚") public static func imageWithColor(_ color: UIColor) -> UIImage
-
Create a 1px image with the given color.
Declaration
Swift
public static func ofColor(_ color: UIColor) -> UIImage
Parameters
color
The of the image to create
-
Deprecated: Please use
roundedRectStretchableImage(borderColor:, fillColor:, borderWidth:, cornerRadius:, scale:)
instead. Create an stretchable rectangle with rounded corners image, with the given parameters.Declaration
Swift
@available(*, deprecated, renamed: "roundedRectStretchableImage(borderColor:borderWidth:fillColor:cornerRadius:scale:﹚") public static func roundedRectStretchableImage( borderColor: UIColor, backgroundColor: UIColor = .clear, lineWidth: CGFloat, radius: CGFloat, scale: CGFloat = 0.0) -> UIImage
Parameters
borderColor
The border color to use for the rectangle.
backgroundColor
The color to use to fill the rectangle.
lineWidth
The width of the border.
radius
The radius of the corners of the rectangle.
0
means the rectangle will not have rounded corners.scale
Please refer to the parameters documentation for
UIGraphicsBeginImageContextWithOptions
for more info.Return Value
The generated stretchable rectangle with rounded corners, with the given parameters.
-
Create an stretchable rectangle with rounded corners image, with the given parameters.
Declaration
Swift
public static func roundedRectStretchableImage( borderColor: UIColor, borderWidth: CGFloat, fillColor: UIColor = .clear, cornerRadius: CGFloat, scale: CGFloat = 0.0) -> UIImage
Parameters
borderColor
The border color to use for the rectangle.
borderWidth
The width of the border.
fillColor
The color to use to fill the rectangle.
cornerRadius
The radius of the corners of the rectangle.
0
means the rectangle will not have rounded corners.scale
Please refer to the parameters documentation for
UIGraphicsBeginImageContextWithOptions
for more info.Return Value
The generated stretchable rectangle with rounded corners, with the given parameters.