gpt4 book ai didi

ios - NSURLConnection 委托(delegate)方法未执行

转载 作者:行者123 更新时间:2023-11-28 22:49:55 26 4
gpt4 key购买 nike

我正在尝试从服务器中提取 ScrollView 的图像。用户放大或缩小 View 后,应下载图像:

- (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 委托(delegate)方法不会被触发并且设置为 subview 的图像仍然是最初下载的图像(当应用程序启动时)。然后,只有在我再次触摸屏幕( ScrollView 滚动)后,NSLog 消息才显示图像已下载。这种行为的原因可能是什么?

编辑:添加了 NSURLConnection 委托(delegate)方法。我尝试了其他几种方法,但它们最终都没有执行委托(delegate)方法。这让我认为这不是关于 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;
}

最佳答案

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

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

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

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

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

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

关于ios - NSURLConnection 委托(delegate)方法未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com