UIImageView + AFNetworking setImageWithURL带动画

Dan*_*pos 22 objective-c uiimageview afnetworking

使用AFNetworking,从服务器下载图像并放入UIImageView非常简单:

[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
Run Code Online (Sandbox Code Playgroud)

如果我想用效果(可能是淡入淡出)替换图像怎么样?

这是因为我想制作带有大量图像的幻灯片.

Rob*_*Rob 45

您可以animateWithDurationsetImageWithURL提供success块的再现结合使用,例如

[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];
Run Code Online (Sandbox Code Playgroud)

或者,如果占位符图像不是空白,您可能希望通过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];
Run Code Online (Sandbox Code Playgroud)

更新:

顺便说一句,如果您担心self在下载完成之前保留图像视图(如果您引用,视图或视图控制器),您可以:

__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];
Run Code Online (Sandbox Code Playgroud)

即使您这样做,图像视图也会保留,直到下载完成,因此您也可以选择取消dealloc视图控制器方法中正在进行的任何下载:

- (void)dealloc
{
    // if MRC, call [super dealloc], too

    [_imageView cancelImageRequestOperation];
}
Run Code Online (Sandbox Code Playgroud)