gpt4 book ai didi

objective-c - UIImageView+AFNetworking setImageWithURL 带动画

转载 作者:太空狗 更新时间:2023-10-30 03:12:13 28 4
gpt4 key购买 nike

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

[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

如果我想用一种效果(可能是淡入淡出)来替换图像呢???

因为我想制作一个有很多图片的幻灯片。

最佳答案

您可以将 animateWithDuration 与提供 success block 的 setImageWithURL 的呈现结合使用,例如

[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 交叉溶解:

[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];

更新:

顺便说一句,如果您担心 ImageView (如果您指的是 self、 View 或 View Controller )在下载完成之前一直保留,你可以:

__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];

即使您这样做了, ImageView 也会保留到下载完成,因此您也可以选择在 View Controller 的 dealloc 方法中取消任何正在进行的下载:

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

[_imageView cancelImageRequestOperation];
}

关于objective-c - UIImageView+AFNetworking setImageWithURL 带动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730138/

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