How IOS cuts pictures into circles

Time:2021-7-25

This example shares the specific code for IOS to cut the picture into a circle for your reference. The specific contents are as follows

Original drawing:

Circular picture clipping effect:

Crop to round picture with border:

Core code:

#import <UIKit/UIKit.h>

@interface UIImage (image)

/**
 *Generate a circular picture
 *
 *@ param image picture to crop
 *
 *@ return the circle picture generated
 */

+ (UIImage *)imageWithClipImage:(UIImage *)image;

/**
 *Generate a circular picture with a border
 *
 *@ param borderw border width
 *@ param bordercolor border color
 *@ param image picture to add border
 *
 *@ return a circular picture with a border generated
 */
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image;

@end
#import "UIImage+image.h"

@implementation UIImage (image)

+ (UIImage *)imageWithClipImage:(UIImage *)image{
+ 
  //1. Open the context with the same size as the original image
  UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
  //2. Set a circular clipping area
  //2.1 draw a circle
  UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  //2.2. Set the circular path as the clipping area
  [path addClip];// Anything beyond the crop area will be cut off
  //3. Draw the picture into the context (cut out the content beyond the clipping area)
  [image drawAtPoint:CGPointZero];
  //4. Remove the picture from the context
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  //5. Close context
  UIGraphicsEndImageContext();

  return newImage;
}

+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image{

  //1. Open a context
  CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
  UIGraphicsBeginImageContextWithOptions(size, NO, 0);
  //2. Draw a big circle and display it
  UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
  [borderColor set];
  [path fill];
  //3. Draw a small circle and set the small circle as the clipping area
  UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
  [clipPath addClip];
  //4. Draw the picture into the context
  [image drawAtPoint:CGPointMake(borderW, borderW)];
  //5. Remove the picture from the context
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  //6. Close context
  UIGraphicsEndImageContext();

  return newImage;
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Token expiration processing

The token is used for interface authentication, but the token has an expiration time set by the back end. When the token expires, data can no longer be requestedThe expiration time set by the backend in the project is 24h. During the test, we can manually modify the token value to invalidate the tokenTreatment method: […]