嗨,我正在使用以下代码使用 Tableview 的 NSURLConnection SendAsynchronousRequest 调用加载图像,但它在 IOS 4.3 上崩溃,但相同的代码适用于 IOS 5。
所以谁能告诉我我必须对IOS 4.3做哪些改变
我已经浏览了以下链接,但对我没有任何帮助。
NSURLConnection sendAsynchronousRequest:queue:completionHandler 在 iOS 4.3 中不起作用
有一堂课叫
图像提取器.h
- (void)fetchImageForURL:(NSURL *)url atIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)table;
图像提取器.m
- (void)fetchImageForURL:(NSURL *)url atIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)table {
// NOTE: url is just relative
// There is an issue on iOS 5 that causes the memory capacity to be set to 0 whenever a UIWebView is
// used for the first time. This will correct that issue.
NSLog(@"in fetchImageForURL %@",url);
if([[NSURLCache sharedURLCache] memoryCapacity] != URLMemoryCachSize)
{
[[NSURLCache sharedURLCache] setMemoryCapacity:URLMemoryCachSize];
}
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0f];
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (cachedResponse)
{
NSData *data = [cachedResponse data];
NSLog(@"from cache");
[self postImageCallbackWithTableView:table atIndexPath:indexPath forURL:url withImageData:data];
}
else
{
returningResponse:&response error:&error];
// NSLog(@"loading synchronously");
[NSURLConnection sendAsynchronousRequest:request
queue:fetcherQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[self postImageCallbackWithTableView:table atIndexPath:indexPath forURL:url withImageData:data];
}];
// [self postImageCallbackWithTableView:table atIndexPath:indexPath forURL:url withImageData:data];
}
}
在 tableview 控制器中,我正在调用以下方法,但它适用于 IOS 4.3,但同样适用于 IOS 5
表视图控制器.m
-viewdidload()
{
[NSURLConnection sendAsynchronousRequest:request queue:fetcherQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//[self postImageCallbackWithTableView:table atIndexPath:indexPath forURL:url withImageData:data];
UIImage *image = [UIImage imageWithData:data];
[self.images setObject:image forKey:index];
[table1 beginUpdates];
[table1 reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
[table1 endUpdates];
}];
}