UIImage(Transform)

@interface UIImage (Transform)

Provide some common method for UIImage. Image process is based on Core Graphics and vImage.

Image Geometry

  • Returns a new image which is resized from this image. You can specify a larger or smaller size than the image size. The image content will be changed with the scale mode.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_resizedImageWithSize:(CGSize)size
                                        scaleMode:(SDImageScaleMode)scaleMode;

    Swift

    func sd_resizedImage(with size: CGSize, scaleMode: SDImageScaleMode) -> UIImage?

    Parameters

    size

    The new size to be resized, values should be positive.

    scaleMode

    The scale mode for image content.

    Return Value

    The new image with the given size.

  • Returns a new image which is cropped from this image.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_croppedImageWithRect:(CGRect)rect;

    Swift

    func sd_croppedImage(with rect: CGRect) -> UIImage?

    Parameters

    rect

    Image’s inner rect.

    Return Value

    The new image with the cropping rect.

  • Rounds a new image with a given corner radius and corners.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius
                                                  corners:(SDRectCorner)corners
                                              borderWidth:(CGFloat)borderWidth
                                              borderColor:
                                                  (nullable UIColor *)borderColor;

    Swift

    func sd_roundedCornerImage(withRadius cornerRadius: CGFloat, corners: SDRectCorner, borderWidth: CGFloat, borderColor: UIColor?) -> UIImage?

    Parameters

    cornerRadius

    The radius of each corner oval. Values larger than half the rectangle’s width or height are clamped appropriately to half the width or height.

    corners

    A bitmask value that identifies the corners that you want rounded. You can use this parameter to round only a subset of the corners of the rectangle.

    borderWidth

    The inset border line width. Values larger than half the rectangle’s width or height are clamped appropriately to half the width or height.

    borderColor

    The border stroke color. nil means clear color.

    Return Value

    The new image with the round corner.

  • Returns a new rotated image (relative to the center).

    Declaration

    Objective-C

    - (nullable UIImage *)sd_rotatedImageWithAngle:(CGFloat)angle
                                           fitSize:(BOOL)fitSize;

    Swift

    func sd_rotatedImage(withAngle angle: CGFloat, fitSize: Bool) -> UIImage?

    Parameters

    angle

    Rotated radians in counterclockwise.⟲

    fitSize

    YES: new image’s size is extend to fit all content. NO: image’s size will not change, content may be clipped.

    Return Value

    The new image with the rotation.

  • Returns a new horizontally(vertically) flipped image.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_flippedImageWithHorizontal:(BOOL)horizontal
                                               vertical:(BOOL)vertical;

    Swift

    func sd_flippedImage(withHorizontal horizontal: Bool, vertical: Bool) -> UIImage?

    Parameters

    horizontal

    YES to flip the image horizontally. ⇋

    vertical

    YES to flip the image vertically. ⥯

    Return Value

    The new image with the flipping.

Image Blending

  • Return a tinted image with the given color. This actually use alpha blending of current image and the tint color.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor;

    Swift

    func sd_tintedImage(with tintColor: UIColor) -> UIImage?

    Parameters

    tintColor

    The tint color.

    Return Value

    The new image with the tint color.

  • Return the pixel color at specify position. The point is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. The image must be CG-based.

    Note

    The point’s x/y should not be smaller than 0, or greater than or equal to width/height.

    Note

    The overhead of object creation means this method is best suited for infrequent color sampling. For heavy image processing, grab the raw bitmap data and process yourself.

    Declaration

    Objective-C

    - (nullable UIColor *)sd_colorAtPoint:(CGPoint)point;

    Swift

    func sd_color(at point: CGPoint) -> UIColor?

    Parameters

    point

    The position of pixel

    Return Value

    The color for specify pixel, or nil if any error occur

  • Return the pixel color array with specify rectangle. The rect is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. The image must be CG-based.

    Note

    The rect’s width/height should not be smaller than or equal to 0. The minX/minY should not be smaller than 0. The maxX/maxY should not be greater than width/height. Attention this limit is different from sd_colorAtPoint: (point: (0, 0) like rect: (0, 0, 1, 1))

    Note

    The overhead of object creation means this method is best suited for infrequent color sampling. For heavy image processing, grab the raw bitmap data and process yourself.

    Declaration

    Objective-C

    - (nullable NSArray<UIColor *> *)sd_colorsWithRect:(CGRect)rect;

    Swift

    func sd_colors(with rect: CGRect) -> [UIColor]?

    Parameters

    rect

    The rectangle of pixels

    Return Value

    The color array for specify pixels, or nil if any error occur

Image Effect

  • Return a new image applied a blur effect.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius;

    Swift

    func sd_blurredImage(withRadius blurRadius: CGFloat) -> UIImage?

    Parameters

    blurRadius

    The radius of the blur in points, 0 means no blur effect.

    Return Value

    The new image with blur effect, or nil if an error occurs (e.g. no enough memory).

  • Return a new image applied a CIFilter.

    Declaration

    Objective-C

    - (nullable UIImage *)sd_filteredImageWithFilter:(nonnull CIFilter *)filter;

    Swift

    func sd_filteredImage(with filter: CIFilter) -> UIImage?

    Parameters

    filter

    The CIFilter to be applied to the image.

    Return Value

    The new image with the CIFilter, or nil if an error occurs (e.g. no enough memory).