UICollectionView不显示使用NSURLRequest获取的项目

时间:2012-12-15 09:44:18

标签: ios facebook json image gallery

我正在使用4个屏幕在ios中创建一个应用程序,其中一个我想作为照片库工作。这个图像是我使用图表从Facebook页面下载的。在我的ViewController类中,在viewDidLoad方法中建立连接url:

(void)viewDidLoad
{
  [super viewDidLoad];  
  NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/NBAStoreNYC/photos"];  
  NSURL *url = [NSURL URLWithString:urlString];  

  // Create NSURLRequest
  NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];  

 //Create conection
 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];  

 if (connection) 
 {  
    NSLog(@"Connecting...");   
    dataJSON = [[NSMutableData alloc] init];  
 } 
 else 
 {    
    NSLog(@"ERROR: Unable to create connection.");  
 }
}

为了下载图片,我已经解析了我返回的JSON,用于获取每个图像的URL ...

-(void)connection:(NSURLConnection )connection didFailWithError:(NSError )error{ NSLog(@"Error: %@", [error localizedDescription]); }

-(void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse )response{ NSLog(@"Received response: %@", response); [dataJSON setLength:0]; }

-(void)connection:(NSURLConnection )connection didReceiveData:(NSData )data { [dataJSON appendData:data]; }

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSError *error;
UIImageView *image1;
NSMutableArray *coleccion=[[NSMutableArray alloc] init];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataJSON options:kNilOptions error:&error];
NSArray *data = [dic objectForKey:@"data"];
if (!dic) {
    NSLog(@"Error parsing JSON");
} else {
    for(NSDictionary *item in [dic objectForKey:@"data"]) {
        for(NSDictionary *attachment in [item objectForKey:@"images"]) {
            NSString *nombre = [attachment objectForKey:@"source"];
            NSURL * imageURL = [NSURL URLWithString:nombre];
            NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
            image1 = [UIImage imageWithData:imageData];
            [items addObject:image1];
        }
    }
}
//[self.view setNeedsDisplay];
}

最后,我使用Collection View创建了我的画廊:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [items count]*5;
}

-(UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
  static NSString *identifier = @"CollectionCell";
  UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
  imageView.image = [items objectAtIndex:indexPath.row%1];
  return cell;
}

现在,当我运行应用程序时没有加载任何图像,为什么?我怎么解决它来显示图像?练习几乎与此相同:http://www.techotopia.com/index.php/An_iPhone_iOS_6_Storyboard-based_Collection_View_Tutorial不同之处在于我想下载facebook图片的图像而不是已将它们加载到项目中

非常感谢。

2 个答案:

答案 0 :(得分:0)

我在这里看到的主要问题是您的项目需要时间加载 - 在主线程上加载它们没有任何意义,因为这只会导致UI无响应。如果您正在寻找不在主线程上执行的一行代码,那么它将是:

 NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

另一个(相关的)问题是collectionView委托调用“cellForItemAtIndexPath”&项目准备好之前的“numberOfItemsInSection”。您需要在UICollectionView实例上执行“reloadData”,以便新的下载项目一旦准备好并插入到您的items数组中就会显示。

希望这有帮助

答案 1 :(得分:0)

我有这个,因为我的物品需要时间加载,加载它们没有任何意义。我的新闻方法是:

- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSInvocationOperation *websOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImages) object:nil];
[queue addOperation:websOperation];
}

-(void)loadImages{
NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/NBAStoreNYC/photos"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
    NSLog(@"Connecting...");
    dataJSON = [[NSMutableData alloc] init];
} else {
    NSLog(@"ERROR: Unable to create connection.");
}
}

但方法:

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSError *error;
UIImageView *image1;
NSMutableArray *coleccion=[[NSMutableArray alloc] init];

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataJSON options:kNilOptions error:&error];
//con esto saco las imagenes de la url
NSArray *data = [dic objectForKey:@"data"];
if (!dic) {
    NSLog(@"Error parsing JSON");
} else {
    for(NSDictionary *item in [dic objectForKey:@"data"]) {
        for(NSDictionary *attachment in [item objectForKey:@"images"]) {
            NSString *nombre = [attachment objectForKey:@"source"];
            NSLog(@"%@", nombre);
            NSURL * imageURL = [NSURL URLWithString:nombre];
            NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
            image1 = [UIImage imageWithData:imageData];
            [items addObject:image1];
        }
    }
}

//[self.view setNeedsDisplay];
}

没有执行,为什么?我已经设置了断点,但这种方法不能进入。