【问题标题】:UIImage does not download URLs, even though it recognizes the correct amount it should downloadUIImage 不下载 URL,即使它识别出它应该下载的正确数量
【发布时间】:2013-11-16 00:05:21
【问题描述】:

我遇到了一个奇怪的字符串/数组问题。当我计算数组中的对象数时(使用 [字符串描述] 获取的值,它返回两个正确数量的对象。当我尝试下载图像时,UIImage 不显示任何内容。

这是来自数组 photoArg 的 URL 日志(如果您尝试使用它们,它会说它们是无效的,因为我更改了几行)

    (
    "\n\"https://scontent-a.xx.fbcdn.net/hphotos-ash2/s720x720/196148_2542310658202194_959795763_n.jpg\"",
    "\n\"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash3/s720x720/576808_2239914766108450_770925407_n.jpg\"\n"
     )    

这是我的 numberOfItemsInSection:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.photoArg count];
}

我的 cellForItemAtIndexPath:

- (TBCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CollectionViewCell";

    TBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *ImageURL = [self.photoArg objectAtIndex:indexPath.item];

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

    [cell.imageView setImage:[UIImage imageWithData:imageData]];

    return cell;
}

我的 TBCollectionViewCell 只是一个标准的 UICollectionViewCell,除了里面有一个 UIImageView。

我认为 URL 不会在 cellForItemAtIndexPath: 方法中下载的原因是它们的格式不正确。如果是这样,我应该如何正确格式化数组数据?

【问题讨论】:

    标签: ios uitableview nsstring nsarray


    【解决方案1】:

    那是因为您的字符串不是有效的 URL。您需要去掉 \n\"\" 以便 dataWithContentsOfURL: 接收有效数据。

    "\n\"https://scontent-a.xx.fbcdn.net/hphotos-ash2/s720x720/196148_2542310658202194_959795763_n.jpg\"",
    

    它不是很漂亮,但这应该适合你。

    NSString *string = @"\n\"https://scontent-a.xx.fbcdn.net/hphotos-ash2/s720x720/196148_2542310658202194_959795763_n.jpg\"";
    NSString *noNewLines = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSString *noEscapes = [noNewLines stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    
    NSURL *validURL = [NSURL URLWithString:noEscapes];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-06
      • 2016-07-04
      • 2022-11-16
      • 2021-03-25
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      相关资源
      最近更新 更多