SoFunction
Updated on 2025-04-03

iOS pictures can be stretchable and undeformed.

In actual development of iOS, if we put a picture with a special shape, especially a picture with rounded corners, in UIButton, when the UIButton changes the size, the picture may be stretched and deformed, we can use it through-(UIImage *)resizableImageWithCapInsets:resizingMode:Method (called this method through the UIImage object and passing the name of the image to be stretched as a parameter) returns a stretchable and undeformable image. Here we write this method into the UIImage class classification to encapsulate it. In the future, we can use it directly in iOS development:

UIImage+

#import <UIKit/>

@interface UIImage (Extension)
/**
  * The name of the image is passed, and a picture that can be stretched and not deformed
  *
  * @param imageName Image Name
  *
  * @return stretchable pictures
  */
+ (UIImage *)resizableImageWithName:(NSString *)imageName;
@end

UIImage+

#import "UIImage+"

@implementation UIImage (Extension)

+ (UIImage *)resizableImageWithName:(NSString *)imageName
{

  // Load the original picture  UIImage *norImage = [UIImage imageNamed:imageName];
  // Get half of the width and height of the original picture  CGFloat w =  * 0.5;
  CGFloat h =  * 0.5;
  // Generate a picture that can stretch the specified position  UIImage *newImage = [norImage resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];

  return newImage;
}
@end

The above is the entire content of this article. I hope you can give you a reference and I hope you can support me more.