【问题标题】:how to get CGSizes of image urls in NSArray如何在 NSArray 中获取图像 url 的 CGSizes
【发布时间】:2014-07-23 12:10:15
【问题描述】:

我有一个NSArray,其中包含NSStrings 的图片网址,如下所示:

NSArray *array = [NSArray arrayWithObjects:@"http://graphics8.nytimes.com/images/2014/07/22/blogs/20140722-lens-mark-slide-SG5C/20140722-lens-mark-slide-SG5C-custom1.jpg", @"http://graphics8.nytimes.com/images/2014/07/22/blogs/20140722-lens-mark-slide-SG5C/20140722-lens-mark-slide-SG5C-custom2.jpg", @"http://graphics8.nytimes.com/images/2014/07/22/blogs/20140722-lens-mark-slide-6N62/20140722-lens-mark-slide-6N62-blog480.jpg", nil];

如何获得每个imageUrlCGSize

我知道如何像这样获取一个 imageUrl 的大小:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://graphics8.nytimes.com/images/2014/07/22/blogs/20140722-lens-mark-slide-SG5C/20140722-lens-mark-slide-SG5C-jumbo.jpg"]]];
CGSize imageSize = image.size;

【问题讨论】:

  • «两个或更多,使用一个for»

标签: ios uiimage nsarray cgsize


【解决方案1】:
for(NSString *stringURL in array)
{
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:stringURL]]];
    CGSize imageSize = image.size;
}

我不建议在主线程中这样做。

如果您想获得大小数组,您应该执行以下操作:

NSMutableArray *sizes = [NSMutableArray array];
for(NSString *stringURL in array)
{
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:stringURL]]];
    CGSize imageSize = image.size;
    [sizes addObject:[NSValue valueWithCGSize:imageSize];
}

之后你可以通过以下方式获取CGSize

CGSize imageSize = [sizes[index] CGSizeValue];

【讨论】:

  • 您应该将大小添加到数组中
  • 我可以为此使用 NSValue。但这不是问题的一部分。
  • 你怎么知道的?他要求从数组中获取值,但您的代码只会返回最新的值。
  • 因为我没有看到它有问题。我的代码不会返回任何值,因为 imageSize 仅在循环内可见。
  • 所以你同意你的代码一点意义都没有?
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 2015-12-26
  • 2022-06-20
  • 2015-08-27
  • 2022-09-27
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
相关资源
最近更新 更多