在Scroll View Crashing方面需要帮助

时间:2022-04-06 02:02:30

I am trying to display about 53 images in Xcode for iphone, but about the 37th image crashes my entire app! If any one sees any errors in my code, I would really appreciate your help. Thank you!!

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

I think I am not releasing my images somewhere...just not sure what to do!

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

#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

Thank you!!!!!

2 个解决方案

#1


How big are images? I suspect the problem is not with code itself, but with memory limitations of iPhone/iPod - if you're loading big images they simply eat all memory and program is closed.

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

#2


Well the most likely reason is that you run out of memory. Put an NSLog message in didReceiveMemoryWarning to check if you are getting a warning for low memory.

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

In any case I would suggest that you lazy load images and not all of the images at once. It will greatly reduce to load time of your app and will also probably solve your memory problems. Use the UIScrollViewDelegate to know when to load images that weren't loaded yet according to the position of the UIScrollView offset value.

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

#1


How big are images? I suspect the problem is not with code itself, but with memory limitations of iPhone/iPod - if you're loading big images they simply eat all memory and program is closed.

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

#2


Well the most likely reason is that you run out of memory. Put an NSLog message in didReceiveMemoryWarning to check if you are getting a warning for low memory.

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

In any case I would suggest that you lazy load images and not all of the images at once. It will greatly reduce to load time of your app and will also probably solve your memory problems. Use the UIScrollViewDelegate to know when to load images that weren't loaded yet according to the position of the UIScrollView offset value.

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