iOS images can be stretched without deforming processing operations
- 2020-06-12 10:42:52
- OfStack
In the actual development of iOS, if we take 1 with pictures (with special shape, especially similar with rounded corners) in UIButton, when UIButton change size is, the picture may be stretched and produce deformation, we can use - (UIImage *) resizableImageWithCapInsets: resizingMode: method (through UIImage object to invoke the method, and the incoming to stretch the image name as a parameter) can implementation returns a tensile deformation of the picture, here we write this method UIImage classes encapsulate it in classification, In the future development of iOS, we can directly use:
UIImage+Extension.h
#import <UIKit/UIKit.h>
@interface UIImage (Extension)
/**
* Pass in the name of the image , return 1 A stretchable picture without deformation
*
* @param imageName Image name
*
* @return Stretchable picture
*/
+ (UIImage *)resizableImageWithName:(NSString *)imageName;
@end
UIImage+Extension.m
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
+ (UIImage *)resizableImageWithName:(NSString *)imageName
{
// Load the original image
UIImage *norImage = [UIImage imageNamed:imageName];
// Gets the width and height of the original image 1 And a half
CGFloat w = norImage.size.width * 0.5;
CGFloat h = norImage.size.height * 0.5;
// Generates an image that can be stretched to a specified position
UIImage *newImage = [norImage resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];
return newImage;
}
@end