我在一个应用程序中有 7 个 ViewController,其中一个将本地 PDF 加载到 UIWebView 中。初始加载没有问题,但是如果我离开那个 ViewController 并返回它,第二次或第三次我在 iPad 上调试时收到“收到内存警告”(我没有在模拟器中遇到过这个问题,但是这并不意味着我最终不会得到它)。我已确保大小写/大写字母与文件和我的代码一致,并且没有收到构建错误或警告。有人告诉我关于 xcode4 需要 @property/@synthesize/dealloc 的混杂信息,所以我尝试了有无它,结果没有改变。
这是我的 .h 文件
#import <UIKit/UIKit.h>
@interface Article6 : UIViewController {
IBOutlet UIWebView *webview;
}
@end
这是我的 .m 文件
#import "Article6.h"
@implementation Article6
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"mfa" ofType:@"pdf" inDirectory:NO];
NSLog(@"path: %@",path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
@end
我正在使用 Storyboard 中的滑动手势,还有一个常规按钮,这两个 segue 都会出现此错误。是的,所有网点都已连接。
最佳答案
在您的 viewDidUnload 方法中查看您自己的评论。这是您需要释放的地方,看起来每次加载 View 时都在创建一个新 View 。所以当你卸载时释放它。我猜每次关闭 webview 时都不会调用 deAlloc。
为了确保我只是简单地使用一些日志来查看您的保留计数并确保您将其保留下来。祝你好运!
关于ios - 在 UIWebView 中加载 PDF 时出现内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014135/