Objective-C:在UITableView中获取图像的UIActivity指标

时间:2009-12-13 22:08:21

标签: objective-c iphone-sdk-3.0

我想知道是否有人知道进入UITableView的最佳方式,我们在左侧有图像,以便图像逐渐显示UIActivity指示器而不是不在那里并且突然出现。我已经在一些应用程序上看到了它并想知道如何做到这一点。

3 个答案:

答案 0 :(得分:3)

我自己没有这样做,但我的想法是创建我自己的UIView子类,处理图像加载时显示UIActivity指示器。然后将该视图的实例添加到表格单元格而不是普通的UIImageViews。

<强>更新: 好吧,正如我之前所说,我自己也没有这样做过。以下只是在浏览器中键入,我不确定你是否遇到任何线程问题,但它应该让你知道如何处理它:

@interface IndicatorImageView : UIView {
    UIImageView *imageView;
    UIActivityIndicatorView *indicator;
    NSString *imageName;
    NSURL *imageURL;
}
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, retain) NSURL* imageURL;
@end

@implementation IndicatorImageView
@synthesize imageName;
@synthesize imageURL;
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:imageView];

        indicator = [[UIActivityIndicatorView alloc] initWithFrame:self.bounds];
        [self addSubview:indicator];
    }
    return self;
}
- (void)setImageName:(NSString *)anImageName {
    if (imageName == anImageName) {
        return;
    }
    [imageName release];
    imageName = [anImageName copy];

    // optionally remove the old image while we're updating
    //imageView.image = nil;

    [indicator startAnimating];
    [self performSelectorInBackground:@selector(updateImageViewUsingImageName) withObject:nil];
} 
- (void)setImageURL:(NSURL *)anImageURL {
    if (imageURL == anImageURL) {
        return;
    }
    [imageURL release];
    imageURL = [anImageURL copy];

    // optionally remove the old image while we're updating
    //imageView.image = nil;

    [indicator startAnimating];
    [self performSelectorInBackground:@selector(updateImageViewUsingImageURL) withObject:nil];
} 
- (void)updateImageViewUsingImageName {
    NSAutoreleasepool *pool = [[NSAutoreleasePool alloc] init];
    imageView.image = [UIImage imageNamed:imageName];
    [indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil];
    [pool release];
}
- (void)updateImageViewUsingImageURL {
    NSAutoreleasepool *pool = [[NSAutoreleasePool alloc] init];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    imageView.image = [UIImage imageWithData:imageData];
    [indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil];
    [pool release];
}
@end

当您设置imageName属性时,图像将在UIActivityIndicatorView动画时在后台加载。加载图像并在UIImageView上设置后,我们会停止为活动指示器设置动画。

答案 1 :(得分:1)

如果我能记得正确的东西,那就是在stanford讲座cs193,iPhone应用程序编程,从Twitter帐户加载个人资料图片时显示的内容。

查看相应的演示文稿(我不记得它是哪一个),看看谈论表格视图的笔记,然后看看演讲的itunesu视频。

http://www.stanford.edu/class/cs193p/cgi-bin/index.php

答案 2 :(得分:1)

子类UIImage并将UIActivityIndi​​cator添加为子视图。在类中添加一些方法来设置图像源,并适当地启动/停止UIActivityIndi​​cator。