gpt4 book ai didi

objective-c - 尝试在 XCode 中淡入从 URL 异步加载的图像

转载 作者:行者123 更新时间:2023-11-28 22:57:31 24 4
gpt4 key购买 nike

我想从 URL 加载图像,然后将它们指定为不同 UIButton 的背景图像。我需要在出现 View 时执行此操作。

我的问题是,当我尝试让图像在加载时淡入时,动画要等到所有图像都加载完毕后才会开始。我认为这是因为动画代码已被读取,但在它有时间执行之前,程序开始加载新图像。

如何让图片一张一张淡入淡出?

下面的代码是用来获取图片(在URL调用downloadImageByPrice)然后做动画。

-(void) obtainImage:(int)i atURL:(NSString *)URLString{

UIImage *image = [self downloadImageByPrice:i atURL:URLString];

// Make images fade in when they have been found
[[_buttonArray objectAtIndex:i] setAlpha:0.0];

[[_buttonArray objectAtIndex:i] setImage:image forState:UIControlStateNormal];

[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.8];

[[_buttonArray objectAtIndex:i] setAlpha:1.0];
[UIView commitAnimations];


}

该方法用于依次加载所有图片。我希望我的循环等到每个图像都出现后再开始加载下一个图像。

-(void) loadAllImagesAtURL:(NSString *)URLString{
for(int i =0; i<[_buttonArray count];i++){
[self obtainImage:i atURL:URLString];
}
}

我尝试过使用选择器或 completion^ 方法,但没有成功,但我对这些概念的理解仍然很低。

谢谢

最佳答案

检查一下:

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) NSMutableArray *buttonArray;
-(void) loadAllImagesAtURL:(NSString *)URLString;
- (UIImage *)downloadImageAtURL:(NSString *)urlString;
@end

ViewController.m

#import "ViewController.h"
@implementation ViewController
@synthesize buttonArray = _buttonArray;
- (void)viewDidLoad {
[super viewDidLoad];
[self setButtonArray:[NSMutableArray array]];
int margin = 20;
int buffer = 8;
int width = 100;
int height = 50;
for (int i = 0; i < 4; i = i + 1) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setAlpha:0.0];
[btn setFrame:CGRectMake(margin + ((width + buffer) * i), margin, width, height)];
[btn setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[[self buttonArray] addObject:btn];
[[self view] addSubview:btn];
}
[self loadAllImagesAtURL:@"https://www.google.com/images/srpr/logo3w.png"];
}
-(void) loadAllImagesAtURL:(NSString *)URLString {
static int i = -1;
i = i + 1;
if (i < [[self buttonArray] count]) {
UIImage *image = [self downloadImageAtURL:URLString];
// Make images fade in when they have been found
[[_buttonArray objectAtIndex:i] setImage:image forState:UIControlStateNormal];
[UIView animateWithDuration:1.0
animations:^{
[[_buttonArray objectAtIndex:i] setAlpha:1.0];
}
completion:^(BOOL finished) {

[self loadAllImagesAtURL:URLString];
}
];
}
}
- (UIImage *)downloadImageAtURL:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
return image;
}
@end

对我来说,这会创建 4 个按钮(最初是不可见的),这些按钮会在下载图像时淡入。这是你想要的吗?

关于objective-c - 尝试在 XCode 中淡入从 URL 异步加载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506929/

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