IOS NSURLConnection 和 AFImageRequestOperation 奇怪的行为

标签 ios nsurlconnection afnetworking

我绝对是个菜鸟,我真的需要你的帮助。我已经寻找解决方案一周了,但我真的不知道该怎么做。 我正在尝试从网络服务下载一些图标(png 图像),并且我已经在其他地方使用了该代码,但仅适用于 1 个图像,而不适用于多个图像。 在本例中我应该下载 15 张图像。 在这两种情况下,使用 AFImageRequestOperation 或 NSURLConnection sendAsynchronousRequest,我到达了所有 15 个图像的末尾,但之后它开始出现一个错误,我真的不知道如何修复,因为它不应该出现在那里! (* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFArray CGPointValue]:发送到实例的无法识别的选择器)。 如果我使用 synchronousRequest 下载 15 张图像,则完全没有问题。 我做错了什么?是我的错误在这里还是其他地方? 非常感谢。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imagePath]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessing``Block:nil                                                                                            success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
         // Save Image
        [images_icon addObject:image ];
        if ([images_icon count]==[allIcons count]) {
            [self saveAllImagesToDisk];
        }
        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
          NSLog(@"%@", [error localizedDescription]);
          UIAlertView* allerta=[[UIAlertView alloc] initWithTitle:@"Attention"
                    message:@"Some problems here.."
                    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
          [allerta show];
        }];
    [operation start];

NSURL *url = [NSURL URLWithString:percorsoImmagine];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {NSLog(@"lungh %d, error=%@ mime=%@",[data length], error, response.MIMEType);
         if ([data length] > 0 && error == nil && ([response.MIMEType isEqualToString:@"image/png"] || [response.MIMEType isEqualToString:@"image/jpeg"])){
             [images_icon addObject:data ];
             if ([images_icon count]==[allIcons count]) {
                [self saveAllImagesToDisk];
             }
         }
     }];

最佳答案

当您使用AFImageRequestOperation时,您将UIImage添加到images_icon,但当您使用NSURLConnection时,您将添加NSDataimages_icon。我们需要查看[self saveAllImagesToDisk]以进一步调查。

关于IOS NSURLConnection 和 AFImageRequestOperation 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15043692/

相关文章:

iphone - AFNetworking:返回 200 且无内容时运行失败 block

ios - 对成员 'upload(_:to:method:headers:interceptor:)' 的模糊引用

swift - NSURLRequest 在没有连接到服务器的情况下超时

ios - AFNetworking 2.0 响应对象处理

iphone - iOS 缓存策略

iphone - 将 NSData 加载到 UIWebView

ios - 使用 AFNetworking 的 Swift 3

ios - Xcode 自动布局指南不出现

ios - UIImage 数组到 PDF 转换内存泄漏

ios - 实例成员不能用于类 NSObject 的类型