作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 iPhone 应用程序出现问题,当我在 iPod 上运行它时,它需要超过 10 分钟才会崩溃并关闭。
因此,我查找了配置文件统计信息,并且似乎是高 Live Bytes,如下图所示:
任何人都可以帮助我减少此 Live Bytes 以避免崩溃?
编辑:
我在调用 viewDidLoad 时使用了三个 for 循环,这三个循环从引用中获取图像,所以我认为它会让应用程序崩溃?
那么在不导致崩溃的情况下执行这三个 for 循环的替代方法是什么?
编辑 2:
这是我的 View 加载,每个循环从不同的文件夹中获取不同的图像,所有这些都是必要的
编辑 3:文件夹 a 有 100 张图片,其中一半是每张图片的缩略图,对于文件夹 b,有 30 张图片,最后是文件夹 c 90 张图片,其中一半是每张图片的缩略图,所有文件夹都在引用中,所以它没有从互联网上获取任何图像...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSInteger x = 6;
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *aPath = [resourcePath stringByAppendingPathComponent:@"a"];
NSArray *aDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:aPath error:nil];
for (NSString *tString in aDirContents) {
if ([tString hasSuffix:@".jpg"]) {
UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
[item setFrame:CGRectMake(x, 5, 60, 60)];
x += 63;
NSString *result = [tString substringToIndex:[tString length] - 4];
[item setTitle:result forState:UIControlStateNormal];
[item setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[item setBackgroundImage:[UIImage imageNamed:tString] forState:UIControlStateNormal];
[item addTarget:self action:@selector(changeFont:) forControlEvents:UIControlEventTouchUpInside];
[aSlider addSubview:item];
}
}
aSlider.frame = CGRectMake(aSlider.frame.origin.x, aSlider.frame.origin.y, x, 69);
[aSliderContainer setContentSize:aSlider.frame.size];
x = 6;
NSString *bPath = [resourcePath stringByAppendingPathComponent:@"b"];
NSArray *bDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bPath error:nil];
for (NSString *tString in bDirContents) {
if ([tString hasSuffix:@".jpg"]) {
UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
[item setFrame:CGRectMake(x, 5, 60, 60)];
x += 63;
NSString *result = [tString substringToIndex:[tString length] - 4];
[item setTitle:result forState:UIControlStateNormal];
[item setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[item setBackgroundImage:[UIImage imageNamed:tString] forState:UIControlStateNormal];
[item addTarget:self action:@selector(changeFont:) forControlEvents:UIControlEventTouchUpInside];
[bSlider addSubview:item];
}
}
bSlider.frame = CGRectMake(bSlider.frame.origin.x, bSlider.frame.origin.y, x, 69);
[bSliderContainer setContentSize:bSlider.frame.size];
// c Slider
x = 6;
NSString *cPath = [resourcePath stringByAppendingPathComponent:@"c"];
NSArray *cDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cPath error:nil];
for (NSString *tString in cDirContents) {
if ([tString hasSuffix:@".jpg"]) {
UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
[item setFrame:CGRectMake(x, 5, 60, 60)];
x += 63;
NSString *result = [tString substringToIndex:[tString length] - 4];
[item setTitle:result forState:UIControlStateNormal];
[item setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[item setBackgroundImage:[UIImage imageNamed:tString] forState:UIControlStateNormal];
[item addTarget:self action:@selector(changeFont:) forControlEvents:UIControlEventTouchUpInside];
[cSlider addSubview:item];
}
}
cSlider.frame = CGRectMake(cSlider.frame.origin.x, cSlider.frame.origin.y, x, 69);
[cSliderContainer setContentSize:cSlider.frame.size];
}
最佳答案
鉴于修改后的问题,您正在将图像从持久存储加载到(大概)一系列 ScrollView 中,有几个想法:
viewDidLoad
代码实际上是问题所在,如果是这样,错误究竟是什么。 viewDidLoad
代码之前):
viewDidLoad
中循环加载图像.它浪费了宝贵的内存,如果您同步执行此操作,您的应用程序甚至可能被看门狗进程杀死,因为您的应用程序始终应该在主队列中响应。如果您收到
0x8badf00d
的异常代码,您可以判断看门狗是否会杀死您的应用程序。 (极客幽默:看门狗报告“吃了不好的食物”;见
TN2151 对一些异常代码的描述)。
UIImageView
SDWebImage
中的任何一个类别或
AFNetworking
.如果您使用
UITableView
或
UICollectionView
,这非常简单。如果您使用
UIScrollView
,那么它需要更多的工作(除非您使用第三方“无限滚动”类)。
@interface InfiniteScrollerButton : NSObject
@property (nonatomic, copy, readonly) NSString *filename; // what is the full path of the image
@property (nonatomic, weak, readonly) UIButton *button; // the UIButton; nil if no button yet created for this icon
@property (nonatomic, readonly) CGRect frame; // the frame that the icon does (or would) occupy
@property (nonatomic, readonly) NSInteger tag; // the tag number for the button
- (id)initWithFilename:(NSString *)filename index:(NSInteger)index;
- (void)addButtonToView:(UIView *)view target:(id)target action:(SEL)action;
- (void)removeButton;
@end
@interface InfiniteScrollerButton ()
@property (nonatomic, copy) NSString *filename;
@property (nonatomic, weak) UIButton *button;
@property (nonatomic) CGRect frame;
@property (nonatomic) NSInteger tag;
@end
@implementation InfiniteScrollerButton
- (id)initWithFilename:(NSString *)filename index:(NSInteger)index
{
self = [super init];
if (self) {
_filename = [filename copy];
_frame = CGRectMake(6 + index * 63, 5, 60, 60);
_tag = index;
}
return self;
}
- (void)addButtonToView:(UIView *)view target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button = button;
// on old devices, even retrieving an image from persistent storage can affect the smoothness
// of the UI, so let's do that asynchronously
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [UIImage imageWithContentsOfFile:self.filename];
dispatch_async(dispatch_get_main_queue(), ^{
button.frame = self.frame;
button.tag = self.tag;
NSString *tString = [self.filename lastPathComponent];
NSString *result = [tString substringToIndex:[tString length] - 4];
[button setTitle:result forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
});
});
}
- (void)removeButton
{
if (self.button)
{
[self.button removeFromSuperview];
self.button = nil;
}
}
@end
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *icons;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadModel];
[self scrollViewDidScroll:self.scrollView]; // call this once, manually, so the initial load of visible images takes place
}
// load the model backing our scrollview
- (void)loadModel
{
self.icons = [NSMutableArray array];
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *aPath = [resourcePath stringByAppendingPathComponent:@"a"];
NSArray *aDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:aPath error:nil];
[aDirContents enumerateObjectsUsingBlock:^(NSString *tString, NSUInteger idx, BOOL *stop) {
InfiniteScrollerButton *icon = [[InfiniteScrollerButton alloc] initWithFilename:[aPath stringByAppendingPathComponent:tString] index:idx];
[self.icons addObject:icon];
}];
InfiniteScrollerButton *lastObject = [self.icons lastObject];
self.scrollView.contentSize = CGSizeMake(lastObject.frame.origin.x + lastObject.frame.size.width + 6, 69);
}
- (void)changeFont:(id)sender
{
NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
static NSInteger kMargin = 100;
// get the CGRect for the visible portion of the scroll view,
// adding a bit of a margin so it, effectively, will also load
// the next UIButton, too
CGRect contentRect = CGRectMake(scrollView.contentOffset.x - kMargin,
scrollView.contentOffset.y,
scrollView.bounds.size.width + kMargin * 2,
scrollView.bounds.size.height);
// iterate through all of the icons
for (InfiniteScrollerButton *icon in self.icons)
{
if (CGRectIntersectsRect(contentRect, icon.frame)) // if the icon should be visible ...
{
if (icon.button == nil) // ... but it's not, then add it
[icon addButtonToView:scrollView
target:self
action:@selector(changeFont:)];
}
else // if the icon is no longer visible ...
{
if (icon.button != nil) // ... but it exists, then remove it
[icon removeButton];
}
}
}
@end
viewDidLoad
不应创建
UIButton
控件,而只是填充支持您的 UI 的模型数组。然后您的 ScrollView 将调用委托(delegate)方法
scrollViewDidScroll
,它将根据需要加载和卸载图像。如果你有成千上万的图像,我可能会建议其他优化,但希望这能说明这个概念。
关于iphone - 如何在 iOS 中减少 "Live Bytes"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17302981/
我是一名优秀的程序员,十分优秀!