2015-03-25 217 views
0

我正在構建一個項目,我第一次使用線程。圖像被覆蓋

我採取了一個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 
+1

是self.imageNature UIImageView?如果是的話,你將所有的圖像設置爲相同的UIImageView – Leonardo 2015-03-25 12:59:03

+0

你有什麼(除了不正常工作)不是一個好的異步下載模式。看看異步NSURLConnection或像AFNetworking或SDWebImage的庫 – 2015-03-25 13:01:01

+0

老兄!我已經理解了你在說什麼,但是告訴我如何才能一個接一個地顯示集合視圖中的所有圖像? – iPeter 2015-03-25 13:01:08

回答

0

他們有很多方法可以做到這一點,但我們首先我解釋,你必須下載一個圖像同步的方式,因爲你沒有什麼管理,所以只要下載第一圖像並添加到你的數組中,爲所有人做這個,你就可以使它工作。

我用過的另一種方法是使用Afneworking,它提供了一種緩存圖像的方法,但仍然需要管理一些小東西。

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
       placeholderImage:(UIImage *)placeholderImage 
         success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success 
         failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 
+0

因爲我是iOs開發新手,所以我還沒有了解AFNetworking。分配給我的任務是線程化。我必須在後臺線程中下載圖像,下載後圖像將顯示在collectionView中。根據我的代碼,下載後可以告訴我,我可以將圖像保存在數組中嗎?如果是,那麼如何? @Retro – iPeter 2015-03-25 13:09:52