.h
#import <UIKit/UIKit.h>
@interface ZHCustomImageView : UIView
//提供接口
@property (nonatomic, strong) UIImage* image;
- (instancetype)initWithImage:(UIImage*)image;
@end
.m
#import "ZHCustomImageView.h"
@implementation ZHCustomImageView
- (instancetype)initWithImage:(UIImage *)image {
if (self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]) {
self.image = image;
}
return self;
}
- (void)setImage:(UIImage *)image {
_image = image;
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
[self.image drawInRect:rect];
}
@end
调用方法与UIImageView相似.