2016-02-19 218 views
1

我正在使用下面的一段代码从网上下载图像并将其添加到数组中。无法从网页动态设置图像到图像视图

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

在运行时,图像从网页不会显示下载。有什么我失踪的?

回答

3

你在做什么是使用[UIImage imageNamed:] 但这将访问阵列recipePhotos的对象和每个对象中的字符串将返回。所以你没有这些名称的图像。 由于存储在数组中的对象是图像ü应该尝试这种

所以尝试做

recipeImageView.image = [recipePhotos objectAtIndex:indexPath.row]

recipeImageView.image = [UIImage imageWithData:[recipePhotos objectAtIndex:indexPath.row]

+0

仅对第一图像其余ü需要与imageNamed加载:因为他们是其中U UR此项目文件存取权限的图像。 – Shravan

1

在阵列recipePhotos仅第一索引包含适当的UIImage,因为它包含的完整路径是"http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/02.jpg"myImage)以及除此之外的只有字符串t的文件路径为http://tcpm.mrlazyinc.com/files/users/themafia/te/beauty/

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

});

尝试NSLog阵列recipePhotos

+0

它在数组内打印一些数字。 –

1

使用SDWebImage类动态从网页下面的文件到你的头文件中设置图像的ImageView的

导入。

#import "UIImageView+WebCache.h" 

导入文件后尝试下面的代码。从下面的链接

[self.imgLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.dictTemp valueForKey:@"camp_image"]]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]]; 

下载库: -

https://github.com/rs/SDWebImage