gpt4 book ai didi

ios - AFNetworking:使用setImageWithURLRequest异步图片下载

转载 作者:行者123 更新时间:2023-11-29 02:57:17 25 4
gpt4 key购买 nike

我有一个图像 slider ,正在使用 setImageWithURLRequest 从 url 下载图像。假设用户在应用程序代码后立即从左向右滑动,然后我必须显示我可以实现的最右边的索引图像,但之后我必须从索引的开头下载图像。如何实现?

for (int i = 0; i < self.items.count; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
UIView *subview = self.view;

UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeCenter;
subview = imageView;

NSURLRequest *imagerequest = [NSURLRequest requestWithURL:[_items objectAtIndex:i]];
__weak UIImageView *weakImageView = (UIImageView *) subview;

[(UIImageView *) subview setImageWithURLRequest:imagerequest
placeholderImage:[UIImage imageNamed:@"placeholder.gif"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
if (!strongImageView) return;

strongImageView.image = image;

}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
//[activityIndicatorView removeFromSuperview];

// do any other error handling you want here
}];

[scrollView addSubview:subview];
}

最佳答案

你可以使用UIImageView+AFNetworking.h:

- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage;

例子:

[imageView setImageWithURL:<url> placeholderImage:<image>];

或者:

- (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;

例子:

[imageView setImageWithURL:<url> placeholderImage:<image> success:<successBlock> failure:<faliureBlock>];

关于ios - AFNetworking:使用setImageWithURLRequest异步图片下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736752/

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