ios - 每次 View 滚动时,将 KIImagePager 与外部图像一起加载

标签 ios objective-c parallax

我正在使用顶部带有 KIImagePager 的 QMBParallaxScrollViewController。每次我滚动它时,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。

我怎样才能改变这种行为?

最佳答案

其实viewcontroller频繁触发arrayWithImages方法是没有问题的。 self.photos 数组在 viewcontroller 加载时填充一次,因此它不会影响性能。在 iOS 上询问数据的方法而不是属性是一种常见的模式。唯一应该考虑的是方法应该尽可能快地返回数据。 arrayWithImages 返回 self.photos,因此它应该没有任何性能问题。

要防止重新加载图像,请使用 NSURLCache。将此添加到您的应用委托(delegate):

- (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

关于ios - 每次 View 滚动时,将 KIImagePager 与外部图像一起加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22746152/

相关文章:

ios - Xcode 9.2 - “Fixed Width Constraints May Cause Clipping” 警告

java - 这是 Objective-C 中 java.util.Random 的正确部分吗

jquery - 仅 300 像素高的页脚有视差效果?

ios - cocos2d 视差触摸

ios - Xcode7与array.filter方法的误解

ios - 点击按钮时分层

IOS移动设备管理安装配置文件失败

iphone - 在Xcode 3.2.2中创建NSManagedObject子类文件

ios - Objective-c iOS 原型(prototype)单元重绘后恢复

java - LibGDX 运动不流畅