试图在XCode中异步淡入从URL加载的图像

时间:2012-05-08 21:39:19

标签: objective-c ios xcode

我想从URL加载图像,然后将它们指定为不同UIButton的背景图像。当视图出现时,我需要这样做。

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

如何让图像一个接一个地淡出?

以下代码用于获取图像(调用downloadImageByPrice atURL),然后执行动画。

-(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];
    }    
}

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

谢谢

1 个答案:

答案 0 :(得分:2)

检查出来:

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个按钮(最初是不可见的),在下载图像时淡入。这是你想要的吗?