iPhone应用程序崩溃没有正当理由?

时间:2010-03-23 15:25:31

标签: iphone iphone-sdk-3.0 crash

我正在开发一个我有桌子的应用程序。在表格单元格中,我有一个imageview(图像通过url显示)和textview / webview。我为每一行启动线程以获取

中的图像
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

方法(如果尚未获得图像)并从数组中设置textview / webview的文本。

当收到图像并且我弹出视图时出现问题,应用程序崩溃时会发出以下消息:

bool _WebTryThreadLock(bool),0x1a0670:尝试从主线程或Web线程以外的线程获取Web锁定。这可能是从辅助线程调用UIKit的结果。现在崩溃......

如果我不发布我添加到单元格中的textview / webview,那么情况会变得更加奇怪,那么每件事都可以正常工作。

修改:当我用text标签替换textview / webview时,不会发生崩溃

希望我的问题清楚。如果有任何事情令人困惑,请评论。我需要解决这个问题。

谢谢,

尼基尔

1 个答案:

答案 0 :(得分:1)

使用线程是一个巨大的错误。如果您有其他解决方案,请尽量避免使用线程!

在您的情况下,只需使用异步NSURLConnection,它将负责下载您的图像,同时不会减慢您的应用;)

以下是代码的一部分:

- (void) startDownload {
    self.activeDownload = [NSMutableData data];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:
                              [NSURL URLWithString:@"blablabla"]] delegate:self];
    self.imageConnection = conn;
    [conn release];
}

#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR DOWNLOADING");
    // Clear the activeDownload property to allow later attempts
    self.activeDownload = nil;

    // Release the connection now that it's finished
    self.imageConnection = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"FINISH DOWNLOAD");

    UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
    self.activeDownload = nil;
    self.imageConnection = nil;

    //do whatever you want with your image

    [image release];
}
相关问题