gpt4 book ai didi

ios - 从 AFImageRequestOperation 随机返回的照片

转载 作者:行者123 更新时间:2023-11-29 03:58:12 25 4
gpt4 key购买 nike

在下面的代码片段中,我使用 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];

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

根本没有收到错误,调试显示每个图像都成功返回,但并非所有图像最终都进入核心数据。

有什么想法为什么会发生这种情况吗?

[编辑结束]

最佳答案

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

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

关于ios - 从 AFImageRequestOperation 随机返回的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16224633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com