UIImageView+AFNetworking setImageWithURL with animation(UIImageView+AFNetworking setImageWithURL 带动画)
问题描述
使用 AFNetworking,从服务器下载图像并放入 UIImageView 非常简单:
With AFNetworking, is very simple to download an image from a server and put into an UIImageView:
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
如果我想用效果(可能会褪色)替换图像呢?
How about if I want to do this replacement of image with an effect (maybe fade)???
因为我想用很多图片制作幻灯片.
It's because I want to make a slideshow with a lot of images.
推荐答案
您可以将 animateWithDuration
与提供 success
块,例如
You can use animateWithDuration
in conjunction with the rendition of setImageWithURL
that supplies the success
block, e.g.
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
self.imageView.alpha = 0.0;
self.imageView.image = image;
[UIView animateWithDuration:0.25
animations:^{
self.imageView.alpha = 1.0;
}];
}
failure:NULL];
或者,如果您的占位符图像不是空白,您可能希望通过 transitionWithView
进行交叉溶解:
Or, if you placeholder image isn't blank, you would probably want to cross dissolve via transitionWithView
:
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[UIView transitionWithView:self.imageView
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imageView.image = image;
}
completion:NULL];
}
failure:NULL];
<小时>
更新:
顺便说一句,如果您担心图像视图(如果您引用 self
,也包括视图或视图控制器)会一直保留到下载完成,你可以:
By the way, if you're concerned about the fact that the image view (and if you refer to self
, the view or the view controller, too) being retained until the download is done, you could:
__weak UIImageView *weakImageView = self.imageView;
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
if (!strongImageView) return;
[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
strongImageView.image = image;
}
completion:NULL];
}
failure:NULL];
即使你这样做了,图像视图也会一直保留到下载完成,因此你也可以选择在视图控制器的 dealloc
方法中取消任何正在进行的下载:
Even if you do that, the image view is retained until the download completes, so you could optionally also cancel any download in progress in the dealloc
method of the view controller:
- (void)dealloc
{
// if MRC, call [super dealloc], too
[_imageView cancelImageRequestOperation];
}
这篇关于UIImageView+AFNetworking setImageWithURL 带动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIImageView+AFNetworking setImageWithURL 带动画
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01