1、两种绘制方式:
首先给出代码:
//
// MyView.h
// 07-绘制图片
//
#import <UIKit/UIKit.h>
@interface MyView : UIView
- (instancetype)initWithImage:(UIImage *) image;
@end
//
// MyView.m
// 07-绘制图片
//
#import "MyView.h"
@interface MyView ()
{
UIImage * _image;
}
@end
@implementation MyView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (instancetype)initWithImage:(UIImage *) image {
if (self = [super init]) {
_image = image;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
// [_image drawAtPoint:rect.origin];
[_image drawInRect:rect];
}
@end
当调用drawInRect时:
当调用drawAtPoint时
3、指定方式绘制图片
测试代码:
//
// MyView.h
// 07-绘制图片
//
#import <UIKit/UIKit.h>
@interface MyView : UIView
- (instancetype)initWithImage:(UIImage *) image;
@end
<pre name="code" class="objc">//
// MyView.m
// 07-绘制图片
//
#import "MyView.h"
@interface MyView ()
{
UIImage * _image;
}
@end
@implementation MyView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (instancetype)initWithImage:(UIImage *) image {
if (self = [super init]) {
_image = image;
}