我正在尝试将一组图像从Web服务加载到UICollectionView
,我正在使用MKNetworkKit
来处理网络操作。
这有点奇怪,因为它在一个场景中起作用而在另一个场景中起作用。请耐心等待,这是一个很长的帖子。
我有一个简单的故事板应用程序,其中UIViewController
嵌入了UICollectionView
作为其主视图(由于UI更改而无法使用UICollectionViewController
我要去稍后再做)。
我创建了一个类,它是MKNetworkEngine
的子类,用于处理从Web服务检索图像的方法。
ImageEngine.h
#import "MKNetworkEngine.h"
@interface ImageEngine : MKNetworkEngine
typedef void (^ImagesResponseBlock)(NSMutableArray *imageURLs);
- (void)allImages:(ImagesResponseBlock)imageURLBlock errorHandler:(MKNKErrorBlock)errorBlock;
@end
ImageEngine.m
#import "ImageEngine.h"
@implementation ImageEngine
- (void)allImages:(ImagesResponseBlock)imageURLBlock errorHandler:(MKNKErrorBlock)errorBlock
{
MKNetworkOperation *op = [self operationWithPath:@"All_Images.php"];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
[completedOperation responseJSONWithCompletionHandler:^(id jsonObject) {
imageURLBlock(jsonObject[@"Images"]);
}];
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
errorBlock(error);
}];
[self enqueueOperation:op];
}
- (NSString *)cacheDirectoryName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *cacheDirectoryName = [documentsDirectory stringByAppendingPathComponent:@"SomeImages"];
return cacheDirectoryName;
}
@end
在具有集合视图的视图控制器类中,
#import "AppDelegate.h"
#import "GridViewController.h"
#import "ImageCell.h"
@interface GridViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *images;
@property (strong, nonatomic) NSString *selectedImageUrl;
@end
@implementation GridViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[ApplicationDelegate.imageEngine allImages:^(NSMutableArray *images) {
self.images = images;
[self.collectionView reloadData];
} errorHandler:^(NSError *error) {
NSLog(@"MKNetwork Error: %@", error.localizedDescription);
}];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *thisImage = self.images[indexPath.row];
self.selectedImageUrl = [NSString stringWithFormat:@"%@%@", @"http://toonmoodz.osmium.lk/", thisImage[@"image"]];
[cell.imageView setImageFromURL:[NSURL URLWithString:self.selectedImageUrl]];
return cell;
}
@end
这很好用。图像按预期加载。
然后我使用MBPullDownController对应用进行了小型UI升级。它只是在集合视图下添加了一个表视图。没有更改网络代码。只是MBPullDownController
的一个新子类,用于在主视图控制器中嵌入集合视图和表视图。
但是当我这样做时,图像根本不会加载。我在ImageEngine类的方法中放置了一个断点,看看它们是否被触发但它永远不会发生。 (奇怪的是这个代码实际上今天早上工作得很好。现在它没有,我完全不知道为什么)。它也不会抛出任何错误或警告。
我上传了两个展示此问题的项目,以便其他人更容易理解。如果有人能帮助我,我会非常感激。在过去的几个小时里,我一直在拔头发。
正常工作的This是涉及该问题的项目的来源。 (当你运行它时,它将显示一个空白的白色视图。它看起来像一个普通的视图,但它是集合视图。我加载了一组本地图像,看它是否正常工作)
谢谢。
答案 0 :(得分:1)
当我在GridViewController
方法中的viewDidLoad
中设置断点时,在控制台中执行po [[UIApplication sharedApplication] valueForKeyPath:@"delegate.imageEngine"]
后,我看到imageEngine
属性等于nil。
在application:didFinishLaunchingWithOptions
viewDidLoad
之后,似乎GridViewController
正在执行。
我从application:didFinishLaunchingWithOptions
self.imageEngine = [[ImageEngine alloc] initWithHostName:@"toonmoodz.osmium.lk"];
[self.imageEngine useCache];
然后我将imageEngine延迟加载器添加到AppDelegate
及其工作http://cl.ly/image/1S172D2e050J
- (ImageEngine*) imageEngine {
if (_imageEngine == nil) {
_imageEngine = [[ImageEngine alloc] initWithHostName:@"toonmoodz.osmium.lk"];
[_imageEngine useCache];
}
return _imageEngine;
}