简体   繁体   English

如何在iOS中重新加载我的视图

[英]How to reload my view in iOS

I have a imageView embedded in a scrollView. 我有一个imageView嵌入在scrollView中。 The imageView needs to be downloaded from the Internet (through flickr api), so I add a thread to handle this. 需要从Internet(通过flickr api)下载imageView,因此我添加了一个线程来处理此问题。 But what am I supposed to do after I finished downloading the image? 但是,下载完图像后我该怎么办? How can I reload my imageView?Here's my code. 如何重新加载imageView?这是我的代码。

In fact, it is working fine. 实际上,它工作正常。 but the imageView's size is (0, 0). 但imageView的大小为(0,0)。 How can I fix that? 我该如何解决?

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (!self.myImage)
    {
        UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [spinner startAnimating];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];
        dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader (photo)", NULL);
        dispatch_async(downloadQueue, ^{
            NSData* data = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:self.photo format:FlickrPhotoFormatLarge]];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.myImage = [UIImage imageWithData:data];
                [self.imageView setImage:self.myImage];
                [self viewDidLoad];
                [self viewWillAppear:NO];
                NSLog(@"imageViewSet!");
                self.navigationItem.rightBarButtonItem = nil;
            });
        });    
        dispatch_release(downloadQueue);
    }
    else
    {
        NSString* imageTitle;
        if ([[self.photo objectForKey:@"title"] length] > 0)
            imageTitle = [self.photo objectForKey:@"title"];
        else if ([[self.photo valueForKeyPath:@"description._content"] length] > 0)
            imageTitle = [self.photo valueForKeyPath:@"description._content"];
        else imageTitle = @"Unknown";
        [[self navigationItem] setTitle:imageTitle];
        self.scrollView.delegate = self;
        self.scrollView.contentSize = self.imageView.image.size;
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    double scale = self.imageView.bounds.size.width * 1.0 / self.myImage.size.width;
    if (scale > self.imageView.bounds.size.height * 1.0 / self.myImage.size.height)
        scale = self.imageView.bounds.size.height * 1.0 / self.myImage.size.height;
    NSLog(@"imgview%g, %g",self.imageView.bounds.size.width, self.imageView.bounds.size.height);
    NSLog(@"img%g, %g",self.myImage.size.width, self.myImage.size.height);
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
    [self.scrollView setZoomScale:scale];
}

The usual way is to call: 通常的方法是致电:

[self.imageView setNeedsDisplay];

Edited to Add: 编辑添加:

I just realized you said the image view is still (0,0). 我刚刚意识到您说图像视图仍然是(0,0)。 From the docs : 文档

Setting the image property does not change the size of a UIImageView. 设置image属性不会更改UIImageView的大小。 Call sizeToFit to adjust the size of the view to match the image. 调用sizeToFit调整视图的大小以匹配图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM