.h文件
#import <UIKit/UIKit.h>
@interface ImageView : UIView
@property (nonatomic ,strong)UIImage * image;
- (instancetype)initWithImage:(UIImage *)image;
@end
.m文件
#import "ImageView.h"
@implementation ImageView
- (instancetype)initWithImage:(UIImage *)image
{
self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];//这里为什么要设置initWithFrame不直接init因为initWithImage会自动初始化一个frame,x和y为0,宽高为图片的宽高
if (self) {
self.image=image;
}
return self;
}
-(void)setImage:(UIImage *)image
{
//先赋值
_image=image;
//重绘
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
//传过来的图片绘制到屏幕上
[self.image drawInRect:rect];
}
@end