我正在使用 ALAssetLibrary 开发类似的程序,例如 iPhone 中的照片。我正在尝试在滚动视图中加载图像。当相册中的图片数量较少时,一切正常。但是当我尝试加载包含 200 多张照片的相册时,我的程序结束了,没有任何错误消息。有人知道这个程序吗?
这是我加载滚动视图的代码:
- (void)loadScrollView
{
for (UIView *v in [scrollview subviews]) {
[v removeFromSuperview];
}
CGRect scrollFrame = [self frameForPagingScrollView];
scrollview = [[UIScrollView alloc] initWithFrame:scrollFrame];
CGRect workingFrame = scrollview.frame;
workingFrame.origin.y = 0;
photoCount = [info count];
for(NSDictionary *dict in info) {
UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[scrollview addSubview:imageview];
[imageview release];
[scrollview setPagingEnabled:YES];
[scrollview setDelegate:self];
[scrollview setAutoresizesSubviews:YES];
[scrollview setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[scrollview setShowsVerticalScrollIndicator:NO];
[scrollview setShowsHorizontalScrollIndicator:NO];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
[self setScrollViewContentSize];
[[self view] addSubview:scrollview];
}
非常感谢!
我个人把我的全部UIImageView
我的物体UIScrollView
,但仅为当前可见的图像设置图像属性(并清除不再可见的图像属性)。如果你有数千张图像,也许这也太浪费了(也许你甚至不想保留UIImageView
对象,即使没有它们image
属性集,大约),但如果您要处理数百个,我发现这是一个很好的简单解决方案,解决了UIImage
对象:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
// all of my other viewDidLoad stuff...
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self loadVisibleImages];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self loadVisibleImages];
}
- (void)loadVisibleImages
{
CGPoint contentOffset = self.scrollView.contentOffset;
CGRect contentFrame = self.scrollView.bounds;
contentFrame.origin = contentOffset;
for (UIImageView *imageView in _imageViews) // _imageViews is (obviously) my array of images
{
if (CGRectIntersectsRect(contentFrame, imageView.frame))
{
imageView.image = ... // set the image property
}
else
{
imageView.image = nil;
}
}
}
这是一些执行许多其他操作的代码的片段,因此显然您的实现将有很大不同,但它显示了如何使用scrollViewDidScroll
确定哪些图像可见并适当地加载/卸载图像。如果您还想删除/添加,您可能可以进一步更改UIImageView
对象也是如此,但显然“这个 imageview 可见”的逻辑必须改变,而不是利用frame
所有的UIImageView
对象。
我不确定我是否正确地阅读了您的代码,但是您是否也有所有的UIImage
字典中也有对象吗?这本身就是对内存的过度使用。我通常将实际图像保存在一些持久存储中(例如我使用Documents
文件夹,尽管你可以使用Core Data
or SQLite
,尽管后两者对大图像造成了显着的性能影响)。我保留在内存中的唯一图像是 UI 主动使用的图像,我将使用NSCache
出于性能原因,我反对保留一些,但我还是从持久存储而不是活动内存中提取它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)