为了方便自己,在写代码时要用到的出示类
#pragma mark -初始化
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)loadView{
[super loadView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#pragma mark -内存管理
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)dealloc
{
[super dealloc];
}
-(void)didReceiveMemoryWarning {
/* This is the view controller's method */
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
if(![self isViewLoaded]){
/* release your custom data which will be rebuilt in loadView or viewDidLoad */
}
}
#pragma mark -判断方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return YES;
}
else {
return NO;
}
}
加载图片的类:
#pragma mark -加载图片
-(UIImageView*)LoadResource:(NSString*)strName ext:(NSString*)strExt
{
// 加载欢迎的声音
NSString* strFileName = [[NSString alloc] initWithFormat:@"%@.%@",strName, strExt];
NSString* path;
path = [[NSBundle mainBundle] pathForResource:strName ofType:strExt];
//判断是否存在这个文件
if (path == nil) {
NSString *message = [NSString stringWithFormat:@"Can't find the resource %@", strFileName];
[self PrintLog:message];
if (strFileName) {
[strFileName release];
strFileName = nil;
return FALSE;
}
}
//开始加载图片
UIImageView *imageView = nil;
imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
NSString *message = [NSString stringWithFormat:@"Begin Load Resource %@!",strFileName];
[self PrintLog:message];
//判断是否加载完成
if (imageView == nil)
{
NSString *message = [NSString stringWithFormat:@"Load Resource %@ Failed!",strFileName];
[self PrintLog:message];
if (strFileName) {
[strFileName release];
strFileName = nil;
return FALSE;
}
}
else
{
NSString *message = [NSString stringWithFormat:@"Load Resource %@ Success!",strFileName];
[self PrintLog:message];
}
[strFileName release];
strFileName = nil;
return imageView;
}
#pragma mark -输出日志
-(void) PrintLog:(NSString*)str
{
NSLog(@"%@",str);
}
初始化与内存管理在iOS开发中的应用
本文详细介绍了iOS开发中初始化、内存管理、视图加载与卸载过程的关键方法,包括初始化、加载视图、内存释放以及错误处理机制。同时提供了加载图片的实用类方法,展示了如何在不同方向下适配界面。通过实例分析,为开发者提供了一套高效、稳定的iOS应用开发指南。

被折叠的 条评论
为什么被折叠?



