【问题标题】:NSURLConnection delegate methods not executingNSURLConnection 委托方法未执行
【发布时间】:2012-09-03 19:32:24
【问题描述】:

我正在尝试从服务器中提取图像以用于滚动视图。用户放大或缩小视图后,应下载图像:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

Ymin=365000+375000*_scrollView.contentOffset.x/(scale*1024);
Ymax=365000+375000*(_scrollView.contentOffset.x/scale+1024/scale)/1024;
Xmin=6635000-260000*(_scrollView.contentOffset.y/scale+748/scale)/748;
Xmax=6635000-260000*_scrollView.contentOffset.y/(scale*748);

[self looYhendus];  //Creates NSURLConnection and downloads the image according to scale, Ymin, Ymax, Xmin and Xmax values

UIImage *saadudPilt=_kaardiPilt;
imageView=[[UIImageView alloc] initWithImage:saadudPilt];
imageView.frame=CGRectMake(_scrollView.contentOffset.x,_scrollView.contentOffset.y,1024,748);
[_scrollView addSubview:imageView];

}

在某些情况下(我不知道在什么条件下)它可以工作,但在某些情况下 NSURLConnection 委托方法不会被触发,并且设置为子视图的图像仍然是最初下载的图像(当应用程序启动时)。然后,只有在我再次触摸屏幕(滚动视图滚动)后,NSLog 消息才显示图像已下载。这种行为的原因可能是什么?

编辑:添加了 NSURLConnection 委托方法。我尝试了其他一些方法,但它们最终都没有执行委托方法。这让我觉得这不是关于 NSURConnection 而是关于 UIScrollView (显然,我可能错了)。

- (void)looYhendus
{   
yhendused=CFDictionaryCreateMutable(
                                    kCFAllocatorDefault, 
                                    0,
                                    &kCFTypeDictionaryKeyCallBacks,
                                    &kCFTypeDictionaryValueCallBacks);

NSString *aadress = [NSString stringWithFormat:@"http://xgis.maaamet.ee/wms-pub/alus?version=1.1.1&service=WMS&request=GetMap&layers=MA-ALUSKAART&styles=default&srs=EPSG:3301&BBOX=%d,%d,%d,%d&width=%d&height=%d&format=image/png",Ymin,Xmin,Ymax,Xmax,512,374];
NSURL *url = [[NSURL alloc] initWithString:aadress];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    andmedServerist = [NSMutableData data];
    CFDictionaryAddValue(
                         yhendused,
                         (__bridge void *)theConnection,
                         (__bridge_retained CFMutableDictionaryRef)[NSMutableDictionary
                                                                    dictionaryWithObject:[NSMutableData data]
                                                                    forKey:@"receivedData"]);
}
CFRunLoopRun();
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[andmedServerist setLength: 0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableDictionary *connectionInfo =
(NSMutableDictionary*)objc_unretainedObject(CFDictionaryGetValue(yhendused, (__bridge void *)connection));
[[connectionInfo objectForKey:@"receivedData"] appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ühenduse viga" message:@"Kõige tõenäolisemalt on kaardiserveril probleeme või puudub seadmel internetiühendus" delegate:self cancelButtonTitle:@"Sulge" otherButtonTitles:nil];
[alert show];
CFRunLoopStop(CFRunLoopGetCurrent());
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableDictionary *connectionInfo =
(NSMutableDictionary*)objc_unretainedObject(CFDictionaryGetValue(yhendused, (__bridge void *)connection));
[connectionInfo objectForKey:@"receivedData"];
andmedServerist=[connectionInfo objectForKey:@"receivedData"];

_kaardiPilt = [UIImage imageWithData: andmedServerist];
CFDictionaryRemoveValue(yhendused, (__bridge void *)connection);

CFRunLoopStop(CFRunLoopGetCurrent());

}

编辑:添加了这个:

- (void)viewDidLoad
{
[super viewDidLoad];
Ymin=365000;
Ymax=740000;
Xmin=6375000;
Xmax=6635000;
[self looYhendus];
UIImage *saadudPilt=_kaardiPilt;
imageView=[[UIImageView alloc] initWithImage:saadudPilt];
imageView.frame=CGRectMake(0,0,1024,748);
[_scrollView addSubview:imageView];
[_scrollView setContentSize: CGSizeMake(1024, 748)];
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 50.0;
_scrollView.delegate = self;
}

【问题讨论】:

  • 如果您在使用 NSURLConnection 委托方法时遇到问题,请发布与这些相关的代码,您显示的唯一代码是 UIScrollView 委托方法。

标签: ios nsurlconnection scrollview


【解决方案1】:

为什么要显式调用 CFRunLoopRun(); 并在 connectionDidFinishLoading:didFailWithError: 方法中停止它? (CFRunLoopStop(CFRunLoopGetCurrent());)

假设您在主线程上执行此操作。您正在停止主线程的运行循环。可以有计时器,ScrollView 使用停止响应的事件,因为您停止了主线程的运行循环。

如果您在主线程上调用NSURLConnection,则无需显式运行它(或停止它)。您可以安排在当前运行循环上运行,这是主线程运行循环。如果您在后台线程上执行此操作,那么您的代码似乎是有效的(尽管如果在单独的线程上调用,您不应该在 didFailWithError: 中显示 UIAlertView)。

更新答案(基于@Shiim 的评论):

viewDidLoad 方法中,您正在调用立即返回的[self looYhendus];(因为您正在使用异步URL 连接进行加载)。所以它按预期工作。将 [scrollView addSubview:imageView] 移动到 connectionDidFinishLoading: 方法,一旦完成下载,它会将您下载的 imageView 数据添加到 scrollView 的子视图。或者你可以考虑使用dispatch_queue's创建一个线程并同步加载URL请求,然后使用dispatch_queue's main queue将添加为子视图的imageView的绘图调度到ScrollView到主线程。

在您的情况下,我的建议是使用 dispatch_queue 重新设计方法。这将使您更好地理解解决问题(在这种情况下),并提高您的代码可读性。

【讨论】:

  • @Shiim 你打电话给 [self looYhendus];在 scrollViewDelegate 方法中,您正在使用该方法进行 UIKit 绘图。我没有看到你的代码在哪里产生了一个线程。
  • 我很糟糕 :( 当我将它与 CATiledLayer 一起使用时,它在后台线程中运行(由于某些原因,这被证明不适合我的任务)。我删除了 CFRunLoopRun() 等并添加了 @987654335 @ 现在来自 viewDidLoad 的初始调用导致空白屏幕。虽然图像已下载,但相应的图像视图未设置为滚动视图子视图(将 viewDidLoad 添加到第一篇文章)。它在缩放后设置,但现在应该是已缩放图像,不是初始图像
  • 感谢您的宝贵时间和回答。我尝试了第一个解决方案,现在它可以工作了(是否有必要将startImmediately: 值添加到NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:?)。不过,我也会尝试检查 GCD 解决方案。
【解决方案2】:

我最近遇到了同样的问题。问题是我将连接放在异步线程中,而连接已经是异步的。

我在这里找到了解决方案:NSURLConnection delegate methods are not called

还有一些链接指向在该线程中遇到类似问题的其他人。

如果我是你,我会尝试简单地使用 [theConnection start] 并使用设置的超时来初始化请求,这样你就不必担心在下载图像之前后台线程会关闭。

例如:

[request setTimeoutInterval:30];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2011-08-12
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多