2012-10-18 19 views
0

当在我的UIImage对象上调用setImageWithURLRequest:时,成功或失败处理程序似乎不会被调用。 SetImageWithURL也没有设置我的形象。如果我使用setImageWithURL:placeholder:,则占位符已设置,但url的图像永远不会被设置。问题与setImageWithURL AFNetworking成功处理程序

请原谅我不能更快发布代码。我曾尝试使用两种方法(与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]; 
+0

你可以发布你用来设置imageview的代码吗?发布示例网址也不会伤害。 –

+0

请在错误块中输出错误信息,而不是“NSLog(@”Log does not show“);” 。然后,你会准确地找到问题。 – user2779354

回答

0

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

1

setImageWithURL:是一个UIImageView类,不能用于设置只对一个UIImageView使用UIImage

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

这里的文档:http://engineering.gowalla.com/AFNetworking/Categories/UIImageView(AFNetworking).html

+0

我实际上使用它来设置imageView。我的错。 – davetw12

+0

发布一些代码,以便我们可以看到发生了什么。 – runmad

+0

我已经添加了代码,伙计们。预先感谢您的帮助。 – davetw12