如何设置dataWithContentsOfURL的超时:url

时间:2012-04-17 19:42:14

标签: objective-c ios

我想以较短的超时时间下载,以便更快,并防止应用程序在连接错误时崩溃。

- (void) CreateTitleView {
    NSURL* url;
    NSData* imageData;
    imageData = [NSData dataWithContentsOfURL:url ];
    UIImage* image = [UIImage imageWithData:imageData];
}

我对目标C不满意,所以我请求你的帮助,这样做。感谢。

2 个答案:

答案 0 :(得分:12)

现在,这是可能的。 API就是这样:

NSURLResponse* urlResponse;
NSError* error;
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
NSData* d = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];

答案 1 :(得分:2)

您无法通过设置超时来控制下载速度。这只会控制您的应用程序在放弃下载之前等待的时间。您应该重构您的应用程序以在后台加载图像数据,以便UI保持响应,直到下载完成。

查看NSURLConnection(sendAsynchronousRequest)或AFNetworking