0

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

请原谅我没有尽快发布代码。我尝试使用两种方法(with 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];
4

2 回答 2

1

setImageWithURL:是 UIImageView 类别,不能用于设置UIImage仅用于 UIImageView。

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

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

于 2012-10-18T20:56:06.157 回答
0

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

于 2012-10-19T23:25:38.513 回答