首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含UIImageView元素的数组- setImageWithURL

包含UIImageView元素的数组- setImageWithURL
EN

Stack Overflow用户
提问于 2012-03-28 07:36:00
回答 1查看 1.4K关注 0票数 0

我有这样的代码

代码语言:javascript
复制
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;“保持一致,这里出了什么问题?

EN

回答 1

Stack Overflow用户

发布于 2012-03-28 07:46:59

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

SIGABRT崩溃是否有任何输出?

尝试以下代码:

代码语言:javascript
复制
// 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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9899209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档