setImageWithURL AFNetworking成功处理程序的问题

时间:2012-10-18 20:22:25

标签: ios ios4 uiimageview nsoperation afnetworking

setImageWithURLRequest:对象上调用UIImage时,似乎不会调用成功或失败处理程序。 SetImageWithURL也没有设置我的图像。如果我使用setImageWithURL: placeholder:,则会设置占位符,但网址中的图片永远不会被设置。

原谅我没有尽快发布代码。我试图使用两种方法(使用AFNetworking)来异步设置我的图像视图:

[_imageView setImageWithURL:[NSURL URLWithString:@"path/to/file"]];

[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/path/to/image.jpg"]] placeholderImage:[UIImage imageNamed:@"your_image_here.jpg"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"Your image request succeeded!");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Your image request failed...");
}];

我已尝试在viewDidLoad中触发这些内容,但图片视图从未加载。

查看setImageWithURLRequest:的实际函数声明后,看来我调用成功或失败块时都没有执行。我已将NSLogs放在代码中,让您了解会发生什么:

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
      placeholderImage:(UIImage *)placeholderImage 
               success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
               failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
NSLog(@"Log shows");

[self cancelImageRequestOperation];

NSLog(@"Log shows");
UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest];

NSLog(@"Log shows");

if (cachedImage) {
NSLog(@"Log does not show");

self.image = cachedImage;
self.af_imageRequestOperation = nil;

if (success) {
    success(nil, nil, cachedImage);
    NSLog(@"Log does not show");

}
} else {
NSLog(@"Log shows");

self.image = placeholderImage;

AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
        self.image = responseObject;
        self.af_imageRequestOperation = nil;
        NSLog(@"Log does not show");
    }

    if (success) {
        success(operation.request, operation.response, responseObject);
        NSLog(@"Log does not show");
    }

    [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
        self.af_imageRequestOperation = nil;
        NSLog(@"Log does not show");
    }

    if (failure) {
        failure(operation.request, operation.response, error);
        NSLog(@"Log does not show");
    }

}];

self.af_imageRequestOperation = requestOperation;

[[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];

2 个答案:

答案 0 :(得分:1)

setImageWithURL:是UIImageView类别,不能用于设置仅在UIImageView上使用的UIImage

[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];

以下是文档:http://engineering.gowalla.com/AFNetworking/Categories/UIImageView(AFNetworking).html

答案 1 :(得分:0)

感谢帮助人员,但是像这里的每个人都注意到的,我的代码没有问题。这个问题实际上与ARC有关。我在AFNetworking文件中关闭了它。打开后,一切正常。