ios - 无法从Web动态将图像设置为imageview

标签 ios objective-c uiimageview uiimage nsdata

我正在使用以下代码从Web下载图像并将其添加到数组。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    UIImage* myImage = [UIImage imageWithData:
                        [NSData dataWithContentsOfURL:
                         [NSURL URLWithString: @"http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/02.jpg"]]];

    recipePhotos = [[NSMutableArray alloc] initWithObjects:myImage, @"2.png", @"3.png", @"4.png", @"5.png", @"6.png", @"6.png", @"8.png", @"9.png", @"10.png", nil];
});

我使用以下代码将图像设置到ui上,
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];

在运行期间,不显示从Web下载的图像。有什么我想念的吗?

最佳答案

您正在使用[UIImage imageNamed:]
但这将访问数组recipePhotos的对象,并且将返回每个对象中的字符串。因此,您没有具有这些名称的图像。
由于存储在数组中的对象是图像,因此应尝试此操作

所以尝试做
recipeImageView.image = [recipePhotos objectAtIndex:indexPath.row]
要么
recipeImageView.image = [UIImage imageWithData:[recipePhotos objectAtIndex:indexPath.row]

关于ios - 无法从Web动态将图像设置为imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35497453/

相关文章:

iphone - 为什么我的 UIView subview 没有在父 View 中呈现? (附代码)

android - React-Native Webview 无法在 iOS 中正常加载 android 中的 ppt、xls、doc 文件

ios - 使 uitableview 的行高适合他的内容

ios - UIScrollView 与纯 AutoLayout 方法,缩放和居中 ImageView

css - 如何将我的背景图像设置为随窗口大小自动调整大小?

ios - iCarousel 上的 carouselCurrentItemIndexDidChange 仅在我滑动时被调用

ios - CATextLayer 文本字体随着文本大小的增加而消失

ios - 从NSMutableArray删除特定项目

ios - 如何在 iOS Swift 中将角半径设置为 UIImage 而不是 UIImageView

ios - 在日期选择器中显示时间设置最长时间和最短时间