gpt4 book ai didi

ios - setImageWithURL AFNetworking 成功处理程序的问题

转载 作者:行者123 更新时间:2023-11-29 04:21:16 25 4
gpt4 key购买 nike

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

请原谅我没有早点发布代码。我尝试使用两种方法(使用 AFNetworking)来异步设置我的 ImageView :

[_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 中触发这些,但 ImageView 从未加载。

查看了 setImageWithURLRequest: 的实际函数声明后,当我调用它时,似乎成功或失败 block 都没有被执行。我在代码中放置了 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];

最佳答案

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

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

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

关于ios - setImageWithURL AFNetworking 成功处理程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com