gpt4 book ai didi

ios - iPad 上的 iCarousel 性能问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:53 25 4
gpt4 key购买 nike

我正在使用 Nick Lockwood 的 iCarousel对于 iPad 应用程序。现在它只是 15 个全屏图像的轮播。

我设置了一个单 View 项目,在 Storyboard 中添加了 iCarousel View ,将它的 View Controller 添加为数据源并将此代码放入数据源方法中:

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 15;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", index]];
UIImageView* imgView = [[UIImageView alloc] initWithImage:img];

return imgView;
}

这行得通,但是当我第一次滚动浏览所有项目时,您会注意到在将新项目添加到轮播时性能受到了一点影响。当我第二次检查所有项目时,就不会发生这种情况。

您可以在这个分析器屏幕截图中明白我的意思。上半场的峰值是我第一次滚动浏览所有图像,然后我再次滚动,没有峰值,也没有性能下降。

我该如何解决这个问题?

enter image description here

编辑

我分离了仪器上的一个峰值,这是调用树

enter image description here

编辑

jcesar建议的代码

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray* urls_aux = [[NSMutableArray alloc] init];
for (int i = 0; i<15; i++) {
NSString *urlPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"captura%d", i] ofType:@"jpg"];
NSURL *url = [NSURL fileURLWithPath:urlPath];

[urls_aux addObject:url];
}
self.urls = urls_aux.copy;

self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeLinear;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {

if (view == nil) {
view = [[[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)] autorelease];
view.contentMode = UIViewContentModeScaleAspectFit;
}

[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:view];
((AsyncImageView *)view).imageURL = [self.urls objectAtIndex:index];

return view;
}

最佳答案

我不认为这是一个非常正统的解决方案,但它确实有效。

我所做的是使用 iCarousel 的方法 insertItemAtIndex:animated: 在设置时一次添加一个图像。然后性能受到影响,但之后一切运行顺利。

- (void)viewDidLoad
{
[super viewDidLoad];

self.images = [[NSMutableArray alloc] init];
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeLinear;

for (int i = 0; i<15; i++) {
UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:@"captura%d.jpg", i]];
[self.images addObject:img];
[self.carousel insertItemAtIndex:i animated:NO];
}
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return self.images.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
UIImage* img = [self.images objectAtIndex:index];
UIImageView* imgView = [[UIImageView alloc] initWithImage:img];

return imgView;
}

关于ios - iPad 上的 iCarousel 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15831148/

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