我正在使用以下代码从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/