| 在实际开发中, 经常用到的扩展.自己总结的经验.
.h 文件
#import <UIKit/UIKit.h>
@interface UIImage (YYExtension)
/**保持不渲染图片*/
@property(nonatomic, strong, readonly)UIImage *originarImage;
/**保护图片四周, 拉伸中间一个像素点*/
@property(nonatomic, strong, readonly) UIImage *resizbleImage;
/**返回一张不被渲染的图片*/
+ (UIImage *)imageWithOriginalName:(NSString *)imageName;
/**保护图片四周, 拉伸中间一个像素点*/
+ (UIImage *)resizbleImage:(NSString *)imageName;
@end
.m 文件
#import "UIImage+YYExtension.h"
@implementation UIImage (YYExtension)
/**不被渲染*/
- (UIImage *)originarImage{
return [self imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
/**保护图片四周, 拉伸中间一个像素点*/
- (UIImage *)resizbleImage{
return [self stretchableImageWithLeftCapWidth:self.size.width * 0.5 topCapHeight:self.size.height * 0.5];
}
/**不被渲染*/
+ (UIImage *)imageWithOriginalName:(NSString *)imageName{
return [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
/**保护图片四周, 拉伸中间一个像素点*/
+ (UIImage *)resizbleImage:(NSString *)imageName{
UIImage *image = [UIImage imageNamed:imageName];
return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}
@end
如何实现
[UIImage imageNamed:@""].originarImage;
[UIImage imageWithOriginalName:@""];
[UIImage imageNamed:@""].resizbleImage;
[UIImage resizbleImage:@""];
注意: 使用之前一定要引入头文件