【发布时间】:2012-10-23 12:43:19
【问题描述】:
我正在开发一个 iPad 应用程序,其中有一个 UIView 的自定义子类,其中包含一个显示 HTML 格式文本的 UIWebView(我将其命名为 EssayView)。我在UIScrollView 的每一页上都有这些UIView 子类的一个实例。用户将其横向滚动以在不同文本之间进行选择,然后向下滚动以阅读文本。滚动视图中有十个 EssayViews。
我的应用应该支持所有方向,但是当用户旋转设备(横向到纵向,反之亦然)时,会抛出内存警告,并且应用通常会崩溃。
我做了一些分析。在运行 iOS 5 的 iPad 3 上,应用程序在显示 EssayScrollView 时消耗 528MB 的虚拟内存,但在运行 iOS 5 的 iPad 1 上它仅消耗 193MB ......它仍然在两者上崩溃。在运行 iOS 6 的 iPad 3 上,应用程序在显示 EssayScrollView 时消耗 640MB 的虚拟内存,但在旋转时不会崩溃。所以问题似乎出在运行 iOS 5 的设备上......
这是我初始化 EssayView 实例的方法:
EssayView.m
@interface EssayView()
@property UIWebView *essayWebView;
@property NSString *essayName;
@property int essayNumber;
@end
@implementation EssayView
- (id)initWithEssayNamed:(NSString *)theEssayName number:(int)theEssayNumber{
self = [super initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
if(self){
essayNumber = theEssayNumber;
essayName = theEssayName;
essayWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
essayWebView.center = CGPointMake(self.bounds.size.width/2, essayWebView.center.y);
[essayWebView setScalesPageToFit:NO];
essayWebView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
essayWebView.delegate = self;
}
return self;
}
这是我在 EssayViews 父视图中的轮换代码:
EssayScrollView.m
- (BOOL)shouldAutorotate{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[scrollView setContentSize:CGSizeMake([essayArray count] * scrollView.frame.size.width, scrollView.frame.size.height)];
[scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width*currentEssay, 0) animated:NO];
}
我在EssayView 中将一组UIViewAutoresizingMasks 分配给UIWebView 的行似乎导致了问题。如果我将其注释掉,则 UI 旋转良好且平滑(当然 UIWebView 不会自动调整大小)。
在 iOS 5 上进行测试时,它会引发内存警告,并在几秒钟后旋转并经常崩溃。但是在 iOS 6 上它不会崩溃。在这两种操作系统上,旋转动画都比平时慢且笨拙。
有没有办法让我的多个 webview 调整大小而不会导致内存警告和崩溃?
【问题讨论】:
-
这个项目是否使用 ARC 并且您是否使用过仪器来检查内存泄漏? (raywenderlich.com/2696/…)
-
是的,我正在使用 ARC。旋转发生时没有内存泄漏。
标签: objective-c ipad uiwebview autoresizingmask