2013-07-18 29 views
22

で、サーバからイメージをダウンロードしてUIImageViewに入れることは非常に簡単です:UIImageView + AFNetworking setImageWithURLはAFNetworkingでアニメーション

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

どのように(私は効果で画像のこの交換を行いたい場合について多分退色する)???

私はたくさんの画像でスライドショーを作りたいからです。オンラインリクエストが成功で終了したときに

答えて

45

は、successブロックを提供するsetImageWithURLのレンディションと組み合わせて使用​​できます。

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

更新:ところで

、あなたがあれば、あなたがイメージが空白でないプレースホルダ場合、あなたはおそらく交差transitionWithViewを経由して溶解するのでしょう

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

あるいは、ダウンロードが完了するまで、画像ビュー(および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]; 

あなたは、ダウンロードが完了するまで画像表示が保持されているので、あなたはまた、任意のビューコントローラのdealloc方法で進行中のダウンロードをキャンセルする可能性があることを行う場合でも:

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

    [_imageView cancelImageRequestOperation]; 
} 
+0

ただし、保持サイクルには注意してください。 –

+0

@MarceloFabriあなたは精巧にできますか?どちらがサイクルを保持する? – timpone

+0

ロブ、ローディングのためにフェードエフェクトを作ることが可能かどうか知っていますか?つまり、0%読み込み=不透明度0.0、100%読み込み - 不透明度1.0 –

3

は0から1までImageViewののアルファをアニメーション化してみてください。もちろん

// You should not call an ivar from a block (so get a weak reference to the imageView) 
__weak UIImageView *weakImageView = self.imageView; 

// The AFNetworking method to call 
[imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://host.com/image1.png"]] placeholderImage:nil] 
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
        // Here you can animate the alpha of the imageview from 0.0 to 1.0 in 0.3 seconds 
        [weakImageView setAlpha:0.0]; 
        [UIView beginAnimations:nil context:NULL]; 
        [UIView setAnimationDuration:0.3]; 
        [weakImageView setAlpha:1.0]; 
        [UIView commitAnimations]; 
        } 
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ 
        // Your failure handle code  
        } 

を使用すると、完了ブロック内であなたが好きな他のアニメーションを使用することができます!

+0

しないでくださいweakImageView.imageをsuccessブロックの画像に設定するのを忘れないでください! – CyberDandy

関連する問題