代码之家  ›  专栏  ›  技术社区  ›  srikanth rongali

在哪里使用uiActivityIndicatorView?

  •  0
  • srikanth rongali  · 技术社区  · 15 年前

    我已经把代码写进去了 -(void)loadView{ } 使用nsurl从Internet获取图像。但在加载图像之前,我需要显示旋转器( UIActivityIndicatorView )

    #import "ImageFromWebViewController.h"
    #define USE_TEST_SERVER 1
    @implementation ImageFromWebViewController
    +(NSString *)fileName 
    {
    #if USE_TEST_SERVER 
        return @"http://happyhyderabad.files.wordpress.com/2009/04/anushka4.jpg";
    #else
        return @"http://nutritionresearchcenter.org/healthnews/wp-content/uploads/2008/07/johnny_depp.jpg";
    #endif
    }
    
    - (void)loadView {
        NSString *urlString = [ImageFromWebViewController fileName];
        NSURL *url = [NSURL URLWithString:urlString];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        imageView = [[UIImageView alloc] initWithImage:image];
        contentView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        [contentView setContentSize:[image size]];
        [contentView addSubview:imageView];
        [imageView setUserInteractionEnabled:NO];
        self.view = contentView;
    }
    
    - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
            }
    
    - (void)dealloc {
        [imageView release];
        [contentView release];
        [super dealloc];
    }
    @end
    

    viewDidLoad 我写代码是为了 活动指示器视图 但是自旋体在加载图像后启动,并且没有停止。

    我应该在哪里编写微调器的代码?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Marc W    15 年前

    您需要先在后台线程中下载图像。在 viewDidLoad ,您需要启动旋转器,然后启动后台线程。隐藏微调器并绘制图像取决于您希望如何进行。下载完成后,可以在后台线程中隐藏微调器,但严格来说,最好不要从主线程以外的任何线程修改用户界面。

    如果您不想费心处理自己的后台线程,请看一下 [NSURLConnection connectionWithRequest:delegate:] . 这将启动它自己的后台线程,以允许您异步加载数据。在这种情况下,仍要启动旋转器 可视负载 ,然后处理 NSURLConnection 在完成数据下载后调用的委托回调方法中,隐藏微调器(因为此时您将回到主线程中,我相信)。