每次视图滚动时都会使用KIImagePager加载外部图像

时间:2014-03-30 15:54:20

标签: ios objective-c parallax

我在顶部使用QMBParallaxScrollViewController和KIImagePager。每次我滚动它,KIImagePager每次都会加载所有图像。

这是我在顶栏中的代码

- (void)viewDidLoad
{
    self.photos = [NSMutableArray array];

    for(NSString* imageURL in self.pictures) {
        [self.photos addObject: [ENDPOINT stringByAppendingString: imageURL]];
    }

    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *) arrayWithImages
{

    return self.photos;
}

- (UIViewContentMode) contentModeForImage:(NSUInteger)image
{
    return UIViewContentModeScaleAspectFill;
}

触发的方法是arrayWithImages和contentModeForImage。

如何更改此行为?

1 个答案:

答案 0 :(得分:1)

实际上,如果viewcontroller经常触发arrayWithImages方法,那也没关系。 self.photos数组在viewcontroller的加载时被填充一次,因此它不会影响性能。它是iOS上常用的模式,用于询问数据的方法,而不是属性。唯一需要考虑的是方法应该尽可能快地返回数据。 arrayWithImages会返回self.photos,因此它应该在没有任何性能问题的情况下工作。

要防止重新加载图片,请使用NSURLCache。将此添加到您的应用代理:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
  NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 
                                                       diskCapacity:20 * 1024 * 1024 
                                                           diskPath:nil];
  [NSURLCache setSharedURLCache:URLCache];
}

如果您需要了解有关NSURLCache的更多信息,请查看这篇精彩的文章http://nshipster.com/nsurlcache