我有一个图像滑块,正在使用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];
}
答案 0 :(得分:1)
你可以使用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>];