@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建根视图控制器
ViewController* rootViewController = [[ViewController alloc] init];
//创建自定义视图
MyView* view = [[MyView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//用自定义视图替换系统默认视图
rootViewController.view = view;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = rootViewController;
return YES;
}
@end
#import "MyView.h"
@implementation MyView
- (void)drawRect:(CGRect)rect {
//填充白色背景
[[UIColor whiteColor] setFill];
UIRectFill(rect);
//创建UIImage图片对象
UIImage *uiImage = [UIImage imageNamed:@"cat"];
//将UIImage图片对象转换为CGImage图片对象
CGImageRef cgImage = uiImage.CGImage;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, -1);//变化坐标
CGContextTranslateCTM(context, 0, -uiImage.size.height);//变化坐标
CGRect imageRect = CGRectMake(0, 0, uiImage.size.width, uiImage.size.height);
CGContextDrawImage(context, imageRect, cgImage);
}
@end