【问题标题】:How to reload my view in iOS如何在 iOS 中重新加载我的视图
【发布时间】:2012-04-08 14:54:18
【问题描述】:

我在 scrollView 中嵌入了一个 imageView。 imageView需要从网上下载(通过flickr api),所以我添加了一个线程来处理这个。但是下载完图片后我该怎么办?如何重新加载我的 imageView?这是我的代码。

事实上,它运行良好。但是 imageView 的大小是 (0, 0)。我该如何解决?

- (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];
}

【问题讨论】:

  • 注释掉dispatch_release(downloadQueue);是否有效?我认为您在执行块之前释放调度队列。
  • 试过了。还是不行..

标签: iphone objective-c ios view reload


【解决方案1】:

通常的方式是调用:

[self.imageView setNeedsDisplay];

编辑添加:

我刚刚意识到你说图像视图仍然是 (0,0)。来自the docs

设置图像属性不会改变 UIImageView 的大小。 调用 sizeToFit 调整视图的大小以匹配图像。

【讨论】:

  • 我试过了。我替换了 [self viewWillAppear:NO];与 [self.imageView setNeedsDisplay];但它不起作用。
  • 是否有任何内容写入控制台?另外,您确定它正在执行该行吗?你能在调试器中停下来吗?
  • 2012-03-24 14:13:49.451 TopPlaces[1511:f803] imgview320, 367 2012-03-24 14:13:49.452 TopPlaces[1511:f803] img0, 0
  • 它正在执行,因为我在右上角有一个旋转轮。
  • 实际上我的问题是我的视图大小很奇怪。图像视图的大小为 (0,0)。
猜你喜欢
  • 2013-08-19
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 2018-12-24
  • 2011-09-17
  • 2012-07-02
相关资源
最近更新 更多