NSThread 停止进程

发布于 2024-11-09 18:54:08 字数 2108 浏览 3 评论 0原文

我正在使用导航基础应用程序。当我转到下一个视图或返回上一个视图时,线程不会停止。有人可以给我一个在视图之间切换时停止线程的解决方案吗?当我切换到下一个或上一个时,应用程序崩溃。我使用这样的线程来下载图像

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

}

I am using a navigation base application. When I go to next view or back to the previous view, the thread does not stop. Can someone give me a solution for stopping the thread when switching between views? When I switch to next or previous, the application crashes. I use thread like this for downloading the image

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2024-11-16 18:54:08

您可以使用实例方法 cancel 取消正在执行的线程

[yourThread cancel];

,或者您可以使用 exit

[yourThread exit];

来终止线程

You can cancel a thread which is being executed using the instance method cancel

[yourThread cancel];

Or you can use exit

[yourThread exit];

which will terminate the thread

一杯敬自由 2024-11-16 18:54:08

您已经在 viewWillAppear 中启动了线程,因此当您在视图控制器之间切换时它将被调用。如果您只想执行一次线程,请尝试将其放入 viewDidLoad 中。

You have started the thread in viewWillAppear, hence it will get called when you switch between your view controllers. If you want to execute your thread only once, then try putting it in viewDidLoad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文