如何在NSArray中获取图像URL的CGSize

时间:2014-07-23 12:10:15

标签: ios uiimage nsarray cgsize

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

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];

如何获得每个CGSize的{​​{1}}?

我知道如何获得一个imageUrl的大小:

imageUrl

1 个答案:

答案 0 :(得分:2)

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];