我正在开发的一个应用程序正在拉入自定义广告。我可以很好地检索广告,网络方面的工作也很正常。我遇到的问题是,当AdController接收到广告时,它会解析JSON对象,然后请求图像。
// Request the ad information
NSDictionary* resp = [_server request:coords_dict isJSONObject:NO responseType:JSONResponse];
// If there is a response...
if (resp) {
// Store the ad id into Instance Variable
_ad_id = [resp objectForKey:@"ad_id"];
// Get image data
NSData* img = [NSData dataWithContentsOfURL:[NSURL URLWithString:[resp objectForKey:@"ad_img_url"]]];
// Make UIImage
UIImage* ad = [UIImage imageWithData:img];
// Send ad to delegate method
[[self delegate]adController:self returnedAd:ad];
}所有这一切都如预期的那样工作,AdController将图像拉入得很好……
-(void)adController:(id)controller returnedAd:(UIImage *)ad{
adImage.image = ad;
[UIView animateWithDuration:0.2 animations:^{
adImage.frame = CGRectMake(0, 372, 320, 44);
}];
NSLog(@"Returned Ad (delegate)");
}当委托方法被调用时,它会将消息记录到控制台,但UIImageView* adImage需要5-6秒才能生成动画。由于应用程序处理广告请求的方式,动画需要是即时的。
隐藏广告的动画是即时的。
-(void)touchesBegan{
[UIView animateWithDuration:0.2 animations:^{
adImage.frame = CGRectMake(0, 417, 320, 44);
}];
}发布于 2011-10-04 10:07:23
如果广告加载发生在后台线程中(最简单的检查方法是[NSThread isMainThread]),那么您不能在同一线程中更新UI状态!大多数UIKit不是线程安全的;当然,当前显示的UIViews不是线程安全的。可能发生的情况是,主线程不会“注意”后台线程中发生的更改,因此在发生其他事情之前,它不会刷新到屏幕上。
-(void)someLoadingMethod
{
...
if (resp)
{
...
[self performSelectorInMainThread:@selector(loadedAd:) withObject:ad waitUntilDone:NO];
}
}
-(void)loadedAd:(UIImage*)ad
{
assert([NSThread isMainThread]);
[[self delegate] adController:self returnedAd:ad];
}https://stackoverflow.com/questions/7641280
复制相似问题