2011-05-26 89 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