0

我正在 ios 中制作一个有 4 个屏幕的应用程序,其中一个我想用作照片库。我使用图形从 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,以便在每个 for..end 中获取图像的 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];
}

最后我用收藏视图创建了我的画廊:

-(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 图形的图像,而不是已经将它们加载到项目中

非常感谢你。

4

2 回答 2

0

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

 NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

另一个(相关)问题是 collectionView 委托在项目准备好之前调用“cellForItemAtIndexPath”和“numberOfItemsInSection”。您需要在 UICollectionView 实例上执行“reloadData”,以便在新下载的项目准备就绪并插入项目数组后出现。

希望这可以帮助

于 2012-12-15T10:47:12.700 回答
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];
}

不执行,为什么?我已经放了断点,但是这个方法中没有输入。

于 2012-12-15T17:45:36.083 回答