从AFImageRequestOperation随机返回的照片

时间:2013-04-25 21:01:28

标签: ios afnetworking nsoperationqueue

在下面的代码片段中,我使用Web服务进行身份验证,并将非常小的照片(文件大小<50kb)检索到核心数据中。这工作相对正常,但随机它不会返回任何照片。

我已经检查了我传递给它的网址,因为每张照片都是正确的,并且正确解析。我需要它每次都返回一张照片。

LogInfo("IMPORTING PHOTO BINARY DATA.");

NSString *photoURL = value;            
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photoURL]];            
AFImageRequestOperation *operation = [AFImageRequestOperation     imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest    *request, NSHTTPURLResponse *response, UIImage *image) {

LogInfo(@"RETRIEVED PHOTO IN setValue.");                
NSData* imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
LogInfo(@"PUTTING THE IMAGE INTO CORE DATA IN setValue.");
[managedObject setValue:imageData forKey:@"personphoto"];

}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

LogInfo(@"ERROR GETTING PHOTO IN setValue.");

}];
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection,    NSURLAuthenticationChallenge *challenge) {
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.strSPUser password:self.strSPPW persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
}];
NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:operation];

[managedObject setValue:value forKey:@"personimageurl"];

每次都会正确设置“personimageurl”,但核心数据中的“personphoto”每次都不会使用图像数据进行更新。

[编辑]

经过一些进一步的测试后,我注意到当我有更高的带宽连接时,更多的图像被下载并存储在核心数据中,而不是我的连接速度较慢。这将指出AFImageRequestOperation性能出现问题。

[修改

我已经改变了我的代码,工作如下:

在MYHTTPCLIENT.M:

- (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier {
NSString* urlString = identifier;

AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
                                                                                       success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
                                      {
                                          LogInfo(@"SUCCESS GETTING PHOTO: %@", response);
                                          completionBlock(image);
                                      }
                                                                                       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

                                                                                           LogInfo(@"ERROR GETTING PHOTO IN downloadImageWithCompletionBlock.");

                                                                                       }];
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.strSPUser password:self.strSPPW persistence:NSURLCredentialPersistenceForSession];
    [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
}];
[self enqueueHTTPRequestOperation:operation];

}

然后我在MYSYNCENGINE.M中调用上面的方法如下:

[[OLHTTPClient sharedClient] downloadImageWithCompletionBlock:^(UIImage *downloadedImage) {
LogInfo(@"RETRIEVED PHOTO IN setValue.");
NSData* imageData = [NSData dataWithData:UIImagePNGRepresentation(downloadedImage)];
[managedObject setValue:value forKey:@"olpersonimageurl"];
LogInfo(@"PUTTING THE IMAGE INTO CORE DATA IN setValue.");   
[managedObject setValue:imageData forKey:@"olpersonphoto"];    
} identifier:photoURL];

现在,当我在高带宽连接上连接时,我得到了大部分返回的图像但是在我第一次调用上述方法时它们永远不会被返回。随后每次调用都会返回图像。

根本没有收到任何错误,调试显示每个图像都成功返回,但并非所有图像都以核心数据结束。

为什么会发生这种情况?

[结束编辑]

1 个答案:

答案 0 :(得分:1)

简易解决方案:不要将图像数据存储在Core Data中。

NSURLCache提供内存和基于磁盘的存储,并遵守HTTP缓存规则。存储每个托管对象的图像URL并按需加载图像可能是一种更好的方法。