2011-05-26 123 views
0

我正在使用導航基礎應用程序。當我進入下一個視圖或回到前一個視圖時,線程不會停止。在視圖之間切換時,有人能給我一個停止線程的解決方案嗎?當我切換到下一個或上一個時,應用程序崩潰。我使用線程這樣下載的圖像NSThread停止進程

- (void)viewWillAppear:(BOOL)animated { 
    AppDeleget= [[UIApplication sharedApplication] delegate]; 
    ProcessView *Process=[[ProcessView alloc] init]; 
    [Process SearchProperty:AppDeleget.PropertyURL page:AppDeleget.Page]; 
    [Process release]; 
    for(NSDictionary *status in AppDeleget.statuses) 
    {  

     NSMutableString *pic_string = [[NSMutableString alloc] initWithFormat:@"%@",[status objectForKey:@"picture"]]; 

     if([pic_string isEqualToString:@""]) 
     { 

      [ListPhotos addObject:@"NA"]; 

     } 
     else 
     { 

      NSString *str= [[[status objectForKey:@"picture"] valueForKey:@"url"] objectAtIndex:0]; 
      [ListPhotos addObject:str]; 


     } 
    } 
    [NSThread detachNewThreadSelector:@selector(LoadImage) toTarget:self withObject:nil]; 

    [AppDeleget.MyProgressView stopAnimating]; 
    [AppDeleget.Progress removeFromSuperview]; 
    [super viewWillAppear:animated]; 
} 
-(void)LoadImage 
{ 
    for(int x=0;x<[ListPhotos count];x++) 
    { 
     NSData *imageData =[ListPhotos objectAtIndex:x]; 
     id path = imageData; 
     NSURL *url = [NSURL URLWithString:path]; 
     NSLog(@"%@",url); 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 
     [self performSelectorOnMainThread:@selector(downloadDone:) withObject:img waitUntilDone:NO]; 

    } 

} 
-(void)downloadDone:(UIImage*)img { 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:count inSection:0]; 

    if(img == nil) 
    { 
     TableCell *cell = (TableCell *)[TableView cellForRowAtIndexPath:indexPath]; 
     cell.myImageView.image=[UIImage imageNamed:@"No_image.png"]; 
     ++count; 
     [TableView reloadData]; 

    } 
    else 
    { 
     TableCell *cell = (TableCell *)[TableView cellForRowAtIndexPath:indexPath]; 
     cell.myImageView.image=img; 
     ++count; 
     [TableView reloadData]; 
    } 

} 

回答

0

可以取消其正在使用的實例方法cancel

[yourThread cancel]; 

或執行的線程可以使用exit

[yourThread exit]; 

這將終止線程

+0

但當我使用[NSThread退出],但它不起作用 – user763657 2011-05-26 05:13:47

+0

你的意思是控制不會出現loadImage功能? – 2011-05-26 06:45:14

+0

當時使用此模擬器掛起 – user763657 2011-05-26 09:24:55

0

Y你已經開始線程viewWillAppear,因此當你在視圖控制器之間切換時它會被調用。如果您只想執行一次線程,請嘗試將其放入viewDidLoad