gpt4 book ai didi

iphone - UIImageView 和 UIScrollView 加载大量图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:51 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 UIImageViewUIScrollView 来显示很多图像(每次大约有 20 张图像,每张图像大约 600px*500px 和大小大约 600kb)。

我使用这段代码来完成这个功能:

// Here is pictures Data;
self.klpArry = self.pictureData;

CGSize size = self.klpScrollView1.frame.size;
for (int i=0; i < [klpArr count]; i++) {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake((size.width * i)+300, 20, 546, 546)];
NSString *filePath = [[NSBundle mainBundle] pathForResource:[klpArr objectAtIndex:i] ofType:@"jpg"];
UIImage *imageData = [[UIImage alloc]initWithData:[NSData dataWithContentsOfFile:filePath]];
[iv setImage:imageData];
iv.backgroundColor = [UIColor grayColor];
[self.klpScrollView1 addSubview:iv];
imageData = nil;
iv = nil;
iv.image = nil;
filePath = nil;
[imageData release];
[filePath release];
[iv release];
}
// show the picture in scrollview;
[self.klpScrollView1 setContentSize:CGSizeMake(size.width * numImage, size.height)];
self.klpScrollView1.pagingEnabled = YES;
self.klpScrollView1.showsHorizontalScrollIndicator = NO;
self.klpScrollView1.backgroundColor = [UIColor grayColor];

但是每次我初始化这个函数,内存都会增加大约5MB。实际上我发布了 UIImageView, UIimageUIScrollView (vi.image=nil, [vi release]) 但它不起作用,分配的内存没有被释放。顺便说一句,我先用了我 friend 的代码 vi.image = nil 然后 vi = nil; 但是图片没有显示在 ScrollView 上。

最佳答案

在我看来,主要问题是您将局部变量设置为 nil然后您继续尝试在 release 等方法中使用这些局部变量等等,但因为它们已被设置为 nil ,这些方法现在什么都不做,而带有 +1 retainCount 的对象(或现在 +2,因为您已将它们添加到您的 View 中)永远不会发布。

因此,我建议如下:

//Here is pictures Data;
self.klpArry = self.pictureData;

CGSize size = self.klpScrollView1.frame.size;
for (int i=0; i < [klpArr count]; i++) {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake((size.width * i)+300, 20, 546, 546)];
NSString *filePath = [[NSBundle mainBundle] pathForResource:[klpArr objectAtIndex:i] ofType:@"jpg"];
UIImage *imageData = [[UIImage alloc]initWithData:[NSData dataWithContentsOfFile:filePath]];
[iv setImage:imageData];
iv.backgroundColor = [UIColor grayColor];
[self.klpScrollView1 addSubview:iv];

// Don't set these to nil, or else subsequent release statements do nothing!
// These statements are actually not necessary because they refer to local
// variables so you don't need to worry about dangling pointers. Make sure
// you're not confusing the purpose of setting a pointer to nil in ARC to
// what you're doing in your non-ARC code.
//
// imageData = nil;
// iv = nil;

// You don't want to set this to nil because if iv is not nil, your image
// will be removed from your imageview. So, not only is this not needed,
// but it's undesired.
//
// iv.image = nil;

// This statement is not necessary for the same reason you don't do it
// to imageData or iv, either. This is probably even worse, though, because
// filePath is not a variable that you initialized via alloc. You should
// only be releasing things you created with alloc (or new, copy, mutableCopy,
// for which you issued a retain statement).
//
// filePath = nil;

[imageData release];

// filePath is a +0 retainCount already, so don't release. You only release
// those items for which you increased retainCount (e.g. via new, copy,
// mutableCopy, or alloc or anything you manually retained).
//
// [filePath release];

[iv release];
}

// show the picture in scrollview;
[self.klpScrollView1 setContentSize:CGSizeMake(size.width * numImage, size.height)];
self.klpScrollView1.pagingEnabled = YES;
self.klpScrollView1.showsHorizontalScrollIndicator = NO;
self.klpScrollView1.backgroundColor = [UIColor grayColor];

因此,您的代码将被简化(并更正)为:

//Here is pictures Data;
self.klpArry = self.pictureData;

CGSize size = self.klpScrollView1.frame.size;
for (int i=0; i < [klpArr count]; i++) {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake((size.width * i)+300, 20, 546, 546)];
NSString *filePath = [[NSBundle mainBundle] pathForResource:[klpArr objectAtIndex:i] ofType:@"jpg"];
UIImage *imageData = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
[iv setImage:imageData];
iv.backgroundColor = [UIColor grayColor];
[self.klpScrollView1 addSubview:iv];
[imageData release];
[iv release];
}

// show the picture in scrollview;
[self.klpScrollView1 setContentSize:CGSizeMake(size.width * numImage, size.height)];
self.klpScrollView1.pagingEnabled = YES;
self.klpScrollView1.showsHorizontalScrollIndicator = NO;
self.klpScrollView1.backgroundColor = [UIColor grayColor];

或者您可以通过使用 autorelease 进一步简化您的代码:

//Here is pictures Data;
self.klpArry = self.pictureData;

CGSize size = self.klpScrollView1.frame.size;
for (int i=0; i < [klpArr count]; i++) {
UIImageView *iv = [[[UIImageView alloc] initWithFrame:CGRectMake((size.width * i)+300, 20, 546, 546)] autorelease];
NSString *filePath = [[NSBundle mainBundle] pathForResource:[klpArr objectAtIndex:i] ofType:@"jpg"];
UIImage *imageData = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:filePath]] autorelease];
[iv setImage:imageData];
iv.backgroundColor = [UIColor grayColor];
[self.klpScrollView1 addSubview:iv];
}

// show the picture in scrollview;
[self.klpScrollView1 setContentSize:CGSizeMake(size.width * numImage, size.height)];
self.klpScrollView1.pagingEnabled = YES;
self.klpScrollView1.showsHorizontalScrollIndicator = NO;
self.klpScrollView1.backgroundColor = [UIColor grayColor];

顺便说一下,声明(带有 autorelease ):

UIImage *imageData = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:filePath]] autorelease];

可以简化为:

UIImage *imageData = [UIImage imageWithContentsOfFile:filePath];

这给你一个 UIImage , +0 retainCount (即您不必 release 它)从您的文件。

所以,最后的几点观察:

  1. 您真的应该回顾和研究 Advanced Memory Management Programming Guide .如果您是内存管理的新手,这本书会是一本密集的读物,但掌握这些概念(尤其是在非 ARC 代码中)至关重要。

  2. 如果您还没有,我鼓励您通过静态分析器(“Product” - “Analyze” 或 shift+command+B)。如果分析器没有为您识别其中的许多(如果不是大多数)问题,我会感到惊讶。通过分析器运行代码时,警告应该为零。

  3. 如果您想将其提升到一个新的水平,您可能希望对内存管理更加保守。管理原则是一个系统设计,在任何给定时间只加载 UI 需要的图像,这不仅涉及 calvinBhai 关于延迟加载图像的极好建议(即,在你的 UI really 需要它们),而且一旦图像滚出屏幕就会主动释放图像。也许你不需要在你的应用程序中担心它,因为你一次只处理 20 张图像,但如果你的任何一个投资组合/画廊扩展到 100 或 1000 张图像,那么将所有这些都保留在任何给定时间的内存都只是一个坏主意。这是一个更高级的概念,因此也许您应该首先关注现有代码的基本内存管理问题,但从长远来看,您可能需要考虑延迟加载和主动释放图像。

关于iphone - UIImageView 和 UIScrollView 加载大量图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com