2012-05-08 58 views
0

我想從一個URL加載圖像,然後將它們分配爲不同的UIButtons的背景圖像。出現視圖時,我需要這樣做。試圖褪色從XCode異步加載的圖像

我的問題是,當我嘗試使圖像在加載時淡入時,直到加載所有圖像纔開始動畫。我認爲這是因爲動畫代碼被讀取,但是在它有時間執行之前,程序開始加載新的圖像。

如何讓圖像一個接一個地褪色?

以下代碼用於獲取圖像(調用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]; 
    }  
} 

我試過使用選擇器或完成^方法沒有運氣,但我對這些概念的理解仍然很低。

謝謝

+0

你應該看看NSThread或NSOperation ... –

回答

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個按鈕(最初是不可見的),在下載圖像時會淡入。這是你想要的嗎?

+0

這可能是我想要的!但是這些圖像是一次出現,還是一個接一個地褪去? – juliensaad

+0

1.他們一次褪色。 2.如果你嘗試了代碼,你會知道這個答案。 :-) – mbm29414

+0

我知道!我會馬上試一下,非常感謝 – juliensaad