gpt4 book ai didi

iphone - 从 AFImageRequestOperation 中的成功 block 返回图像

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

我正在尝试编写一个方便的函数,它将接受图像标识符并使​​用 AFNetworking 的 AFImageRequestOperation 下载图像。该函数正确下载图像,但我无法在成功 block 中返回 UIImage。

-(UIImage *)downloadImage:(NSString*)imageIdentifier
{
NSString* urlString = [NSString stringWithFormat:@"http://myserver.com/images/%@", imageIdentifier];

AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
NSLog(@"response: %@", response);
return image;
}
failure:nil];

[operation start];

}

return image; 行给了我错误:

Incompatible block pointer types sending 'UIImage *(^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)' to parameter of type 'void (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)' 

对发生的事情有什么想法吗?我很乐意打电话

UIImage* 照片 = [downloadImage:id_12345];

最佳答案

AFNetworking 图像下载操作是异步的,您无法在操作开​​始时对其进行分配。

您尝试构建的函数应该使用委托(delegate)或 block 。

- (void)downloadImageWithCompletionBlock:(void (^)(UIImage *downloadedImage))completionBlock identifier:(NSString *)identifier {
NSString* urlString = [NSString stringWithFormat:@"http://myserver.com/images/%@", identifier];

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

[operation start];
}

这样称呼

// start updating download progress UI
[serverInstance downloadImageWithCompletionBlock:^(UIImage *downloadedImage) {
myImage = downloadedImage;
// stop updating download progress UI
} identifier:@""];

关于iphone - 从 AFImageRequestOperation 中的成功 block 返回图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14970035/

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