NSURLConnection sendAsynchronousRequest用于ios 4中的图像崩溃

时间:2013-11-12 17:20:04

标签: ios objective-c nsurlconnection

您好我正在使用以下代码为Tableview使用NSURLConnection SendAsynchronousRequest调用加载图像,但它崩溃了IOS 4.3但相同的代码适用于IOS 5.

所以任何人都可以告诉我,我必须为IOS 4.3提供哪些支持

我已经通过以下链接,但没有任何对我有用。

NSURLConnection sendAsynchronousRequest:queue:completionHandler not working in iOS 4.3

有一个名为

的课程

imagefetcher.h

- (void)fetchImageForURL:(NSURL *)url atIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)table;

imagefetcher.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控制器中我正在调用follwing方法,但是对于IOS 4.3来说它是crsahes,但对IOS 5来说同样适用

tableviewcontroller.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];
                    }];
                }

1 个答案:

答案 0 :(得分:2)

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

或者,正如NSURLConnectionDataDelegate提供的答案所示,编写自己的方法,提供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` }]; }];

sendAsynchronousRequest

最重要的是,只需使用iOS 4.3中提供的方法(例如sendSynchronousRequest)替换您对{{1}}的来电。