在Scroll View Crashing方面需要帮助

时间:2009-06-17 02:07:43

标签: iphone xcode crash

我试图在Xcode中为iphone显示大约53张图片,但是大约第37张图片崩溃了我的整个应用程序!如果有人在我的代码中看到任何错误,我将非常感谢您的帮助。谢谢!!

我想我不会在某个地方发布我的图片......只是不知道该怎么做!

#import "MyProjectViewController.h"

@implementation MyProjectViewController


@synthesize scrollView1;

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


const CGFloat kScrollObjHeight = 320.0;
const CGFloat kScrollObjWidth = 480.0;
const NSUInteger kNumImages = 53;


- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;

curXLoc += (kScrollObjWidth);
}
}

[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled = YES;




NSUInteger i;

for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@"c1_%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[scrollView1 addSubview:imageView];
[imageView release];

}

[self layoutScrollImages];

}



- (void)dealloc
{
[scrollView1 release];

[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}


@end

谢谢!!!!!

2 个答案:

答案 0 :(得分:2)

图片有多大?我怀疑问题不在于代码本身,而在于iPhone / iPod的内存限制 - 如果您正在加载大图像,他们只需吃掉所有内存并关闭程序。

答案 1 :(得分:1)

最可能的原因是你内存不足。在didReceiveMemoryWarning中放置一条NSLog消息,检查是否收到内存不足的警告。

在任何情况下,我都会建议您延迟加载图像,而不是一次性加载所有图像。它将大大减少你的应用程序的加载时间,也可能会解决你的内存问题。使用UIScrollViewDelegate根据UIScrollView偏移值的位置知道何时加载尚未加载的图像。