具有UIImageView元素的数组 - setImageWithURL

时间:2012-03-27 23:36:00

标签: objective-c xcode

我有这样的代码

arrayWithImages = [[NSMutableArray alloc] init];

NSEnumerator *enumForNames = [arrayWithNames objectEnumerator];
NSEnumerator *enumForURLs = [arrayWithURLs objectEnumerator];

id objName, objURL;

while(objName = [enumForNames nextObject]) {
    objURL = [enumForURLs nextObject];

    UIImageView *anImage = nil;
    [anImage setImageWithURL:[NSURL URLWithString:objURL]];

    (...)

    [arrayWithImages addObject:anImage];

}

每次我让SIGABRT符合“[arrayWithImages addObject:anImage];” 怎么了?

1 个答案:

答案 0 :(得分:0)

我在setImageWithURL上看不到UIImageView方法。这是从哪里来的?

SIGABRT是否有任何输出崩溃?

试试这段代码:

// Download the image
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:objURL]];
// Make an UIImage out of it
UIImage *anImage = [UIImage imageWithData:imageData];
// Create an image view with the image
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];

// Check to make sure it exists
if (imageView != nil) {
    // Add it to your array
    [arrayWithImages addObject:imageView];
else {
    NSLog(@"Image view is nil");
}

请注意,您应该异步下载图像以避免挂起循环。 This blog post讨论异步图像下载。

此外,如果您知道[enumForURLs nextObject];将返回NSString(甚至更好,NSURL),您应该将objURL指定为NSString(或NSURL)类型。