31

SDWebView 画像を使用していますが、リモートから画像を取得しているときに、アクティビティ インジケーターをプレースホルダーとして表示したいと考えています。

ここでマレクの答えを試しましたHow to show an activity indicator in SDWebImage、しかしそれは

UIImage *cachedImage = [manager imageWithURL:url];

廃止されました。

このライブラリを使用して、画像の読み込み中にアクティビティ インジケーターを挿入する方法を教えてくれる人はいますか?

編集

Michael Frederick の指摘に従って、私はこのコードになり、すべてが正常に機能しています。

UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
activityIndicator.center = CGPointMake(self.tipImage.frame.size.width /2, self.tipImage.frame.size.height/2);
[imageView setImageWithURL:[NSURL URLWithString:imageString]
          placeholderImage:nil options:SDWebImageProgressiveDownload
                   success:^(UIImage *image) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }
                   failure:^(NSError *error) {  [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }];

[imageView addSubview:activityIndicator];
4

18 に答える 18

47

このフォークは、この機能をサポートしています。差分を見てください。

しかし、一般的には、そのフォークを使用しなくてもかなり簡単に実行できます。

__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
activityIndicator.center = imageView.center;
activityIndicator.hidesWhenStopped = YES;
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                        success:^(UIImage *image) { [activityIndicator removeFromSuperview]; }
                        failure:^(NSError *error) { [activityIndicator removeFromSuperview]; }];

[imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
于 2012-07-01T16:49:45.200 に答える
22

これはあなたがそれを行う方法です:

[imageView setIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imageView setShowActivityIndicatorView:true];
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"defaultl_image"]];

于 2015-07-22T10:19:53.013 に答える
5

sdwebimage の最新バージョンでは、これがより良い方法だと思います。

[self.customImageView setImageWithURL:imageURL
                             placeholderImage:nil
                                      options:nil
                                     progress:^(NSUInteger receivedSize, long long expectedSize) { [self.activityIndicator startAnimating]; }
                                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { [self.activityIndicator stopAnimating]; }];

注: プロパティを設定していない場合、明らかに self.activityindicator は機能しません。

代わりに、Michael Frederick の例を使用して activityindicator を作成します。

于 2013-03-27T14:18:16.847 に答える
5

上記のコードは少し間違っています。行「activityIndi​​cator.center = imageView.center」は、進行状況ビューを中央に配置するための正しい座標を提供しません。私にとっては、セルの下にインジケーターが表示されていました。

注 - SDWebImage コードの最新バージョンを使用しているため、方法が若干異なります。imageView にも UIButton を使用しています

これを試して:

NSURL *url = [NSURL URLWithString:@"www.someimageurl.com"];
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = cell.imageView.bounds;
[cell.imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
[cell.imageView setImageWithURL:url forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    [activityIndicator removeFromSuperview];
}];
于 2013-03-13T22:03:38.193 に答える
3

上記の SWIFT 4 と同じコード:

let activityIndicator = UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    activityIndicator.center = addImage.center
    activityIndicator.hidesWhenStopped = true

    addImage.sd_setImage(with: URL(string: feed.image), completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
        activityIndicator.removeFromSuperview()
    })

    addImage.addSubview(activityIndicator)
    activityIndicator.startAnimating()
于 2018-02-20T17:01:46.833 に答える
2

SDWebImage を使用してイメージ URL をロードするために Swift 2.0 用に更新されたソリューション

imageView.setShowActivityIndicatorView(true)

imageView.setIndicatorStyle(.White)
于 2015-11-02T09:19:53.117 に答える
2
[cell.dreamImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dream.thumbnail]] placeholderImage:[UIImage imageNamed:@"dream_bg_2.png"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    CGFloat domandeFloat = [[NSNumber numberWithInt: receivedSize] floatValue];
    CGFloat corretteFloat = [[NSNumber numberWithInt: expectedSize] floatValue];


    float currentProgress = domandeFloat/corretteFloat;

     NSLog(@"progress %f",currentProgress);
    cell.progressView.progress = currentProgress;

    //[self.dreamsTableView reloadData];


} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

    cell.progressView.hidden = YES;
}];
于 2014-12-31T11:28:21.300 に答える
1

Can Ürek の回答に基づいて、SDWebImage フレームワークを変更せずに、複数のアプリケーションで使いやすくするためにカテゴリを作成することをお勧めします。

ヘッダー ファイル:

#import <UIKit/UIKit.h>

#import <SDWebImage/UIImageView+WebCache.h>

@interface UIImageView (WebCacheWithActivityIndicator)

- (void)setImageShowingActivityIndicatorWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock;

@end

実装ファイル:

#import "UIImageView+WebCacheWithActivityIndicator.h"

@implementation UIImageView (WebCacheWithActivityIndicator)

- (void)setImageShowingActivityIndicatorWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock
{
    UIActivityIndicatorView* activityIndication = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    [activityIndication setFrame:CGRectMake((self.frame.size.width - activityIndication.frame.size.width) / 2 , (self.frame.size.height - activityIndication.frame.size.height) / 2 , activityIndication.frame.size.width , activityIndication.frame.size.width)];
    [self addSubview:activityIndication];

    [activityIndication startAnimating];

    [self setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

        if(completedBlock)
        {
            completedBlock(image,error,cacheType);
        }

        [activityIndication stopAnimating];
        [activityIndication removeFromSuperview];
    }];
}
@end

それがあなたを助けることを願っています

乾杯

于 2014-02-20T21:46:53.767 に答える
0

ユーザーがリストを繰り返し上下にスクロールすると、リストがかなり遅くなります。cellForItemAtIndexPath を繰り返し呼び出していると、ActivityIndi​​cator が無限に追加されていることがわかりました。

これを解決するには、アクティビティ インジケーターにタグを追加します。

activityIndicator.tag = 9999;

UIActivityIndi​​cator が作成される前に、次の行を追加して以前の activityindicator を削除してから新しいものを追加します。

UIView* toDeleteLoader = [cell viewWithTag:9999];
if(toDeleteLoader != nil){
    [toDeleteLoader removeFromSuperview];
}

注: タグの内容は、セルで使用されない任意の数字にすることができます。

于 2015-07-16T04:37:46.027 に答える
0

別の解決策は、次を使用してアニメーション画像をプレースホルダー画像として使用することです。

[UIImage animatedImageNamed:@"animated_placeholder" duration:1]

于 2014-04-26T21:43:57.433 に答える
0

SDWebImage 5.0 は、SDWebImageActivityIndi​​cator クラスを使用したアクティビティ インジケーターの表示をサポートしています。

let imageView = UIImageView()
imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
于 2020-09-25T08:43:24.887 に答える