Iphone 从AFImageRequestOperation中的成功块返回图像
我正在尝试编写一个方便的函数,该函数将接受图像标识符,并使用AFNetworking的AFImageRequestOperation下载图像。该函数正确下载图像,但我无法在成功块中返回UIImageIphone 从AFImageRequestOperation中的成功块返回图像,iphone,ios,objective-c,cocoa,afnetworking,Iphone,Ios,Objective C,Cocoa,Afnetworking,我正在尝试编写一个方便的函数,该函数将接受图像标识符,并使用AFNetworking的AFImageRequestOperation下载图像。该函数正确下载图像,但我无法在成功块中返回UIImage -(UIImage *)downloadImage:(NSString*)imageIdentifier { NSString* urlString = [NSString stringWithFormat:@"http://myserver.com/images/%@", imageIdent
-(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];
}
返回图像代码>行显示错误:
Incompatible block pointer types sending 'UIImage *(^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)' to parameter of type 'void (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)'
你知道发生了什么事吗?我很想打电话给你
UIImage*photo=[下载图像:id_12345]代码>网络映像下载操作是异步的,您不能在操作开始时分配它
您试图构建的函数应该使用委托或块
- (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:@""];