- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在开发一个简单的 iPad 应用程序(Lion 上的 iOS 5.1.1 和 XCode 4.3.3),它可以水平滚动图像。它使用延迟加载,但不幸的是,它由于内存不足而崩溃,大约 3/4 通过图像的方式。我已经使用 XCode 分析器和 Instruments 来查找和修复任何泄漏,但它仍然崩溃。崩溃日志如下:
Incident Identifier: 91B65E89-4CCE-4CB5-975F-365D23D802E5
CrashReporter Key: 659f640ad55a7dfbaf0305305e0b8f0fb36a6c41
Hardware Model: iPad1,1
OS Version: iPhone OS 5.1.1 (9B206)
Kernel Version: Darwin Kernel Version 11.0.0: Sun Apr 8 21:51:26 PDT 2012; root:xnu-1878.11.10~1/RELEASE_ARM_S5L8930X
Date: 2012-06-29 14:51:58 -0700
Time since snapshot: 49 ms
Free pages: 610
Active pages: 0
Inactive pages: 694
Throttled pages: 43266
Purgeable pages: 0
Wired pages: 18518
Largest process: GBTL
Processes
Name UUID Count resident pages
GBTL <d26b776787493174ae14f454a11b746d> 31295 (jettisoned) (active)
debugserver <2408bf4540f63c55b656243d522df7b2> 134
installd <0f9e14173c503a8089f7f8cd0329a1a0> 403
atc <1e5f2a595709376b97f7f0fa29368ef1> 605 (jettisoned)
notification_pro <373a488638c436b48ef0801b212593c4> 96
notification_pro <373a488638c436b48ef0801b212593c4> 99
notification_pro <373a488638c436b48ef0801b212593c4> 96
afcd <c3cc9d594b523fd1902fb69add11c25d> 133
MobileMail <eed7992f4c1d3050a7fb5d04f1534030> 1004 (jettisoned)
ptpd <62bc5573db7a352ab68409e87dc9abb9> 356 (jettisoned)
mediaserverd <f03b746f09293fd39a6079c135e7ed00> 404 (jettisoned)
iapd <0a747292a113307abb17216274976be5> 285 (jettisoned)
DTMobileIS <1383b8ed7d373b59a0839e89ab947089> 899
notification_pro <373a488638c436b48ef0801b212593c4> 99
dataaccessd <473ff40f3bfd3f71b5e3b4335b2011ee> 735 (jettisoned)
springboardservi <b74f5f58317031e9aef7e95744c816ca> 283
syslog_relay <b07876a121a432d39d89daf531e8f2bd> 62
notification_pro <373a488638c436b48ef0801b212593c4> 98
syslog_relay <b07876a121a432d39d89daf531e8f2bd> 66
notification_pro <373a488638c436b48ef0801b212593c4> 98
filecoordination <0f34c714b10d35318f0d445f1d953386> 98
geod <976e1080853233b1856b13cbd81fdcc3> 127
absinthed.K48 <04b40efabeac392ba63cba7256b86bb0> 74
notifyd <f6a9aa19d33c3962aad3a77571017958> 181
aosnotifyd <8cf4ef51f0c635dc920be1d4ad81b322> 369
BTServer <31e82dfa7ccd364fb8fcc650f6194790> 186
CommCenterClassi <041d4491826e3c6b911943eddf6aaac9> 274
SpringBoard <c74dc89dec1c3392b3f7ac891869644a> 4878 (active)
aggregated <a12fa71e6997362c83e0c23d8b4eb5b7> 356
apsd <e7a29f2034083510b5439c0fb5de7ef1> 630
configd <ee72b01d85c33a24b3548fa40fbe519c> 304
fairplayd.K48 <47deae2d0fb23b6594b0128353e8b48d> 115
fseventsd <914b28fa8f8a362fabcc47294380c81c> 175
imagent <9c3a4f75d1303349a53fc6555ea25cd7> 361
locationd <cf31b0cddd2d3791a2bfcd6033c99045> 833
mDNSResponder <86ccd4633a6c3c7caf44f51ce4aca96d> 188
mediaremoted <327f00bfc10b3820b4a74b9666b0c758> 201
lockdownd <b06de06b9f6939d3afc607b968841ab9> 216
powerd <133b7397f5603cf8bef209d4172d6c39> 133
syslogd <7153b590e0353520a19b74a14654eaaa> 97
wifid <3001cd0a61fe357d95f170247e5458f5> 258
UserEventAgent <dc32e6824fd33bf189b266102751314f> 362
launchd <5fec01c378a030a8bd23062689abb07f> 123
**End**
相关代码如下:
#import "GBTLViewController.h"
@implementation GBTLViewController
@synthesize scrollView;
@synthesize pageControl;
@synthesize pageImages;
@synthesize pageViews;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self loadVisiblePages];
if (!pageControlBeingUsed) {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[newPageView release];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = NO;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = NO;
self.pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"480X_cover.jpg"],
[UIImage imageNamed:@"GBTL1.jpg"],
[UIImage imageNamed:@"GBTL2.jpg"],
[UIImage imageNamed:@"GBTL3.jpg"],
[UIImage imageNamed:@"GBTL4.jpg"],
[UIImage imageNamed:@"GBTL5.jpg"],
[UIImage imageNamed:@"GBTL6.jpg"],
[UIImage imageNamed:@"GBTL7.jpg"],
[UIImage imageNamed:@"GBTL8.jpg"],
[UIImage imageNamed:@"GBTL9.jpg"],
[UIImage imageNamed:@"GBTL10.jpg"],
[UIImage imageNamed:@"GBTL11.jpg"],
[UIImage imageNamed:@"GBTL12.jpg"],
[UIImage imageNamed:@"GBTL13.jpg"],
[UIImage imageNamed:@"GBTL14.jpg"],
[UIImage imageNamed:@"GBTL15.jpg"],
[UIImage imageNamed:@"GBTL16.jpg"],
[UIImage imageNamed:@"GBTL17.jpg"],
[UIImage imageNamed:@"GBTL18.jpg"],
[UIImage imageNamed:@"GBTL19.jpg"],
[UIImage imageNamed:@"GBTL20.jpg"],
nil];
NSInteger pageCount = self.pageImages.count;
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageViews = [[[NSMutableArray alloc] init] autorelease];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
[self loadVisiblePages];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSInteger pageCount = self.pageImages.count;
for (NSInteger i=0; i<pageCount; i++) {
[self purgePage:i];
}
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * pageCount, pagesScrollViewSize.height);
[self loadVisiblePages];
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
@end
如果有人能找出内存问题发生的位置以及如何解决它,我将不胜感激。谢谢!
最佳答案
不知道这些是多大尺寸的图像,但有许多处理此类情况的策略:
1) 你真的需要将图像加载到 NSArray 中吗?根据您的文件命名方案,您似乎可以构建一些逻辑,根据需要将其从您的包中拉出,然后在屏幕外立即丢弃。
2) 有时在@autorelease block 中做这种事情是有利的。看这里: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsautoreleasepool_Class/Reference/Reference.html
3) 你可能想看看这个应用程序缩小大图像以适应低分辨率屏幕的例子: http://developer.apple.com/library/ios/#samplecode/LargeImageDownsizing/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011173
关于iphone - 应用程序崩溃,内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11269798/
我正在通过 labrepl 工作,我看到了一些遵循此模式的代码: ;; Pattern (apply #(apply f %&) coll) ;; Concrete example user=> (a
我从未向应用商店提交过应用,但我会在不久的将来提交。 到目前为止,我对为 iPhone 而非 iPad 进行设计感到很自在。 我了解,通过将通用PAID 应用放到应用商店,客户只需支付一次就可以同时使
我有一个应用程序,它使用不同的 Facebook 应用程序(2 个不同的 AppID)在 Facebook 上发布并显示它是“通过 iPhone”/“通过 iPad”。 当 Facebook 应用程序
我有一个要求,我们必须通过将网站源文件保存在本地 iOS 应用程序中来在 iOS 应用程序 Webview 中运行网站。 Angular 需要服务器来运行应用程序,但由于我们将文件保存在本地,我们无法
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
假设我有一个安装在用户设备上的 Android 应用程序 A,我的应用程序有一个 AppWidget,我们可以让其他 Android 开发人员在其中以每次安装成本为基础发布他们的应用程序推广广告。因此
Secrets of the JavaScript Ninja中有一个例子它提供了以下代码来绕过 JavaScript 的 Math.min() 函数,该函数需要一个可变长度列表。 Example:
当我分别将数组和对象传递给 function.apply() 时,我得到 NaN 的 o/p,但是当我传递对象和数组时,我得到一个数字。为什么会发生这种情况? 由于数组也被视为对象,为什么我无法使用它
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP转换格林威治时间函数DateDiff()应用由作者收集整理,如果你
我正在将列表传递给 map并且想要返回一个带有合并名称的 data.frame 对象。 例如: library(tidyverse) library(broom) mtcars %>% spl
我有一个非常基本的问题,但我不知道如何实现它:我有一个返回数据框,其中每个工具的返回值是按行排列的: tmp<-as.data.frame(t(data.frame(a=rnorm(250,0,1)
我正在使用我的 FB 应用创建群组并邀请用户加入我的应用群组,第一次一切正常。当我尝试创建另一个组时,出现以下错误: {"(OAuthException - #4009) (#4009) 在有更多用户
我们正在开发一款类似于“会说话的本”应用程序的 child 应用程序。它包含大量用于交互式动画的 JPEG 图像序列。 问题是动画在 iPad Air 上播放正常,但在 iPad 2 上播放缓慢或滞后
我关注 clojure 一段时间了,它的一些功能非常令人兴奋(持久数据结构、函数式方法、不可变状态)。然而,由于我仍在学习,我想了解如何在实际场景中应用,证明其好处,然后演化并应用于更复杂的问题。即,
我开发了一个仅使用挪威语的应用程序。该应用程序不使用本地化,因为它应该仅以一种语言(挪威语)显示。但是,我已在 Info.plist 文件中将“本地化 native 开发区域”设置为“no”。我还使用
读完 Anthony's response 后上a style-related parser question ,我试图说服自己编写单体解析器仍然可以相当紧凑。 所以而不是 reference ::
multicore 库中是否有类似 sapply 的东西?还是我必须 unlist(mclapply(..)) 才能实现这一点? 如果它不存在:推理是什么? 提前致谢,如果这是一个愚蠢的问题,我们深表
我喜欢在窗口中弹出结果,以便更容易查看和查找(例如,它们不会随着控制台继续滚动而丢失)。一种方法是使用 sink() 和 file.show()。例如: y <- rnorm(100); x <- r
我有一个如下所示的 spring mvc Controller @RequestMapping(value="/new", method=RequestMethod.POST) public Stri
我正在阅读 StructureMap关于依赖注入(inject),首先有两部分初始化映射,具体类类型的接口(interface),另一部分只是实例化(请求实例)。 第一部分需要配置和设置,这是在 Bo
我是一名优秀的程序员,十分优秀!