我想在我的iPhone应用程序中使用CMS功能。我想从互联网上加载文本和图像,并将它们应用到UIImageView,UIButton和UITextView。但是我收到了一个错误。目前,代码可以正常加载图像。我在viewDidAppear代码中有文本代码但是用户在加载文本之前无法与屏幕交互,因此我将其移动到带有Image的loadInfo方法,但它不喜欢这样并且给了我一个错误的访问权限错误。并在控制台中打印以下内容:
2011-05-08 21:26:13.770 Fraction Calculator Lite[2184:6b03] bool _WebTryThreadLock(bool), 0x62338d0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
任何人都知道我的问题可能是什么?
谢谢!
这是我的代码:
- (void)viewDidAppear:(BOOL)animated {
[self performSelectorInBackground:@selector(loadInfo) withObject:nil];
}
-(void) loadInfo {
NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
NSError *error;
NSString *internetTest = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/internettest.txt"] encoding:NSUTF8StringEncoding error:&error];
if ([internetTest isEqualToString: @"Internet Connection Complete"]) {
UIImage *img1 = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/adimage.png"]]];
[image1 setImage:img1];
[img1 release];
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/adtext.txt"] encoding:NSUTF8StringEncoding error:&error];
NSString *string2 = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://lindahlstudios.com/cms/price.txt"] encoding:NSUTF8StringEncoding error:&error];
[adText setText:string]; //Thread 8: Program received signal: "EXC_BAD_ACCESS"
[price setTitle:string2 forState:UIControlStateNormal];
}
[arPool release];
}
答案 0 :(得分:0)
您无法在后台线程上调用UIKit操作。我建议你在后台线程上获取数据,然后在主线程中更新它。
使用块来简化编码,
在后台线程中检索NSData后,通过块将其简单地分配给主线程中的UIImage等,
//get NSData from URL
dispatch_async(dispatch_get_main_queue(), ^{
//set assignments for UIImage here.
});