程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-AFImageRequestOperation返回圖片

ios-AFImageRequestOperation返回圖片

編輯:編程綜合問答
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];

最佳回答:


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:@""];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved