starwar2012 2013-02-20 01:45 采纳率: 0%
浏览 4691
已采纳

AFImageRequestOperation返回图片

要写一个应用,用来接受图片标示符,并且使用 AFNetworkingAFImageRequestOperation 下载图片。

目前下载已经成功了,但是在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* photo = [downloadImage:id_12345];
  • 写回答

1条回答

  • redCoral_ 2013-02-20 03:22
    关注

    AFNetworking图片下载操作是异步的,你不能在操作开始的时候指定它。

    你构建的函数应该用delegates和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:@""];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘