Я создаю проект, в котором я использую потоки в первый раз.Изображения перезаписываются
Я взял collectionView, где хочу показать изображения. Я получил 10 URL-адресов изображений в массиве. Теперь после загрузки изображения будут отображаться в виде коллекции.
Но когда я запускаю проект, изображения идут, но изображения непрерывно переписываются.
Я смущен тем, как показать все изображения в виде коллекции.
Мой код
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.arrImages = @[@"http://helpyourselfimages.com/wp-content/uploads/2014/10/Nature-Pictures-HD1.jpg",
@"http://helpyourselfimages.com/wp-content/uploads/2014/10/Beautiful-Island-Wallpaper.jpg",
@"http://hdwallpapers4u.eu/wallpaper_3840x2160/booty_hose_sofa_thongs_girl_beautiful_nature_ultra_3840x2160_hd-wallpaper-242177.jpg",
@"http://www.pageresource.com/wallpapers/wallpaper/chelsea-logo-nature-hd-beauty.jpg",
@"http://dowehwall.com/wp-content/uploads/2015/01/hd-wallpaper-beautiful-nature-hd-wallpaper-nature-beautiful-hd-426201-download-this-wallpaper-use-for-facebook-cover-edit-this-wallpapers.jpg",
@"http://imgstocks.com/wp-content/uploads/2013/12/Beautiful-nature-cool-images-background-hd-wallpaper-beautiful-nature.jpg",
@"http://ghost2-gbj.rhcloud.com/content/images/2013/Dec/beautiful_nature_wallpapers_for_desktop_high_definition_wallpaper.jpg",
@"http://www.hdwallpapersos.com/wp-content/uploads/2014/08/nike-air-max-nature-beautiful-pictures.jpg",
@"http://www.3dwallhd.com/wp-content/uploads/2013/02/white_tiger_beautiful-wide.jpg",
@"http://mobiledady.com/wp-content/uploads/2013/04/Beautiful-HD-Nature-Wallpapers-For-Desktop-2013-2014-9.jpg"];
[self fetchData];
}
-(void)fetchData{
dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL);
for (NSString *urlString in self.arrImages) {
dispatch_async(imageQueue, ^{
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if (!self.imageNature) return;
dispatch_async(dispatch_get_main_queue(), ^{
[self.imageNature setImage:image];
});
});
}
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.arrImages count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
self.imageNature = (UIImageView *)[cell.contentView viewWithTag:1];
return cell;
}
- (IBAction)btnAlrt:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"BOOM!!" message:@"Main Thread Is Running" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
Is self.imageNature a UIImageView? Если да, вы устанавливаете все изображения в один и тот же UIImageView. – Leonardo
. У вас есть (кроме того, что не работает должным образом) не очень хороший шаблон для асинхронной загрузки. Взгляните на асинхронные NSURLConnection или библиотеки, такие как AFNetworking или SDWebImage –
Чувак! Я понял, что вы говорите, но просто скажите мне, как я могу показать все изображения в виде коллекции один за другим? – iPeter