在下面的代码片段中,我正在使用 Web 服务进行身份验证,并将非常小的照片(文件大小 < 50kb)检索到核心数据中。这工作相对不错,但随机它不会返回任何照片。
我已经检查了我为每张照片传递给它的 URL 是否正确并正确解析。我每次都需要它来返回照片。
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];
现在,当我连接到高带宽连接时,我会返回大部分图像,但在我第一次调用上述方法时它们从未返回。每次后续调用都会返回图像。
根本没有收到任何错误,调试显示每个图像都成功返回,但并非所有图像最终都在核心数据中。
任何想法为什么会发生这种情况?
[结束编辑]