1

嗨,我正在使用以下代码使用 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];
                    }];
                }
4

1 回答 1

2

如果您查看的文档sendAsynchronousRequest它需要 iOS 5。如果您需要支持 iOS 4.3,则必须使用connectionWithRequest:delegate:orinitWithRequest:delegate:然后实现这些NSURLConnectionDataDelegate方法(虽然需要做更多的工作,但它提供了其他优势,例如能够监控进度或在需要时取消请求)。

或者,正如在其他问题中提供的答案所暗示的那样,编写您自己的方法来提供sendAsynchronousRequest功能但实际上调用sendSynchronousRequest.

或者,只需将您的电话替换为sendAsynchronousRequest

[NSURLConnection sendAsynchronousRequest:request queue:fetcherQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    // do something with `data`, `error`, and `response`
}];

通过调用它sendSynchronousRequest,您将在某个NSOperationQueue队列上执行。因此,首先,为您的操作队列定义一个属性:

@property (nonatomic, retain) NSOperationQueue *networkQueue;

然后初始化它,例如viewDidLoad

self.networkQueue = [[NSOperationQueue alloc] init];
self.networkQueue.name = @"com.domain.app.networkqueue";
self.networkQueue.maxConcurrentOperationCount = 4;

然后您可以使用该网络操作队列来调用sendSynchronousRequest

[self.networkQueue addOperationWithBlock:^{
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    // assuming you want to interact with your UI and or synchronize changes to your model, dispatch this final processing back to the main queue

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // do something with `data`, `error`, and `response`
    }];
}];

最重要的是,只需将您的调用替换为iOS 4.3 中可用的sendAsynchronousRequest方法,例如。sendSynchronousRequest

于 2013-11-12T17:30:54.700 回答