UIImage
extension UIImage
-
Initializes and returns the image object with the specified data in a thread-safe manner.
It has been reported that there are thread-safety issues when initializing large amounts of images simultaneously. In the event of these issues occurring, this method can be used in place of the
init?(data:)
method.Declaration
Swift
public static func af_threadSafeImage(with data: Data) -> UIImage?
Parameters
data
The data object containing the image data.
Return Value
An initialized
UIImage
object, ornil
if the method failed. -
Initializes and returns the image object with the specified data and scale in a thread-safe manner.
It has been reported that there are thread-safety issues when initializing large amounts of images simultaneously. In the event of these issues occurring, this method can be used in place of the
init?(data:scale:)
method.Declaration
Swift
public static func af_threadSafeImage(with data: Data, scale: CGFloat) -> UIImage?
Parameters
data
The data object containing the image data.
scale
The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.
Return Value
An initialized
UIImage
object, ornil
if the method failed.
-
Returns whether the image is inflated.
Declaration
Swift
public var af_inflated: Bool { get set }
-
Inflates the underlying compressed image data to be backed by an uncompressed bitmap representation.
Inflating compressed image formats (such as PNG or JPEG) can significantly improve drawing performance as it allows a bitmap representation to be constructed in the background rather than on the main thread.
Declaration
Swift
public func af_inflate()
-
Returns whether the image contains an alpha component.
Declaration
Swift
public var af_containsAlphaComponent: Bool { get }
-
Returns whether the image is opaque.
Declaration
Swift
public var af_isOpaque: Bool { get }
-
Returns a new version of the image scaled to the specified size.
Declaration
Swift
public func af_imageScaled(to size: CGSize) -> UIImage
Parameters
size
The size to use when scaling the new image.
Return Value
A new image object.
-
Returns a new version of the image scaled from the center while maintaining the aspect ratio to fit within a specified size.
The resulting image contains an alpha component used to pad the width or height with the necessary transparent pixels to fit the specified size. In high performance critical situations, this may not be the optimal approach. To maintain an opaque image, you could compute the
scaledSize
manually, then use theaf_imageScaledToSize
method in conjunction with a.Center
content mode to achieve the same visual result.Declaration
Swift
public func af_imageAspectScaled(toFit size: CGSize) -> UIImage
Parameters
size
The size to use when scaling the new image.
Return Value
A new image object.
-
Returns a new version of the image scaled from the center while maintaining the aspect ratio to fill a specified size. Any pixels that fall outside the specified size are clipped.
Declaration
Swift
public func af_imageAspectScaled(toFill size: CGSize) -> UIImage
Parameters
size
The size to use when scaling the new image.
Return Value
A new image object.
-
Returns a new version of the image with the corners rounded to the specified radius.
Declaration
Swift
public func af_imageRounded(withCornerRadius radius: CGFloat, divideRadiusByImageScale: Bool = false) -> UIImage
Parameters
radius
The radius to use when rounding the new image.
divideRadiusByImageScale
Whether to divide the radius by the image scale. Set to
true
when the image has the same resolution for all screen scales such as @1x, @2x and @3x (i.e. single image from web server). Set tofalse
for images loaded from an asset catalog with varying resolutions for each screen scale.false
by default.Return Value
A new image object.
-
Returns a new version of the image rounded into a circle.
Declaration
Swift
public func af_imageRoundedIntoCircle() -> UIImage
Return Value
A new image object.
-
Returns a new version of the image using a CoreImage filter with the specified name and parameters.
Declaration
Swift
public func af_imageFiltered(withCoreImageFilter name: String, parameters: [String : Any]? = nil) -> UIImage?
Parameters
name
The name of the CoreImage filter to use on the new image.
parameters
The parameters to apply to the CoreImage filter.
Return Value
A new image object, or
nil
if the filter failed for any reason.