gpt4 book ai didi

ios - setImageWithURLRequest :placeholderImage:success:failure No block is called

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:59:41 28 4
gpt4 key购买 nike

我正在尝试使用 AFNetworking 2.0 从 url 异步获取图像

我的问题是既没有调用成功也没有调用失败

我已经在浏览器中检查了我的 URL,所以问题不在这里

UIImageView *imgv = [[UIImageView alloc] init];
[imgv setImageWithURLRequest:myURL
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
/*some code*/
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
/*some code*/
];

当我深入了解 setImageWithUrlRequest:placeholderImage:success:failure

   __weak __typeof(self)weakSelf = self;
self.af_imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
self.af_imageRequestOperation.responseSerializer = self.imageResponseSerializer;
[self.af_imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) {
if (success) {
success(urlRequest, operation.response, responseObject);
} else if (responseObject) {
strongSelf.image = responseObject;
}

if (operation == strongSelf.af_imageRequestOperation){
strongSelf.af_imageRequestOperation = nil;
}
}

[[[strongSelf class] sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) {
if (failure) {
failure(urlRequest, operation.response, error);
}

if (operation == strongSelf.af_imageRequestOperation){
strongSelf.af_imageRequestOperation = nil;
}
}
}];

我的代码得到了 [self.af_imageRequestOperation setCompletionBlockWithSuccess:^( AFHTTPRequestOperation *operation, id responseObject) {
成功,但我注意到 __strong __typeof(weakSelf)strongSelf = weakSelf; 都是 nil

有什么想法吗?

最佳答案

你的 imgv 永远不会被保留,所以它会在当前范围之后被释放,所以在你的 block 中 weakSelf 会在图像成功下载时被释放,这就是你得到 nil 的原因。

关于ios - setImageWithURLRequest :placeholderImage:success:failure No block is called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741975/

28 4 0