iphone - 如何在 iOS 中重新加载我的 View

标签 iphone objective-c ios view reload

我在 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];
}

最佳答案

通常的方式是调用:

[self.imageView setNeedsDisplay];

编辑添加:

我刚刚意识到你说 ImageView 仍然是 (0,0)。来自 the docs :

Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.

关于iphone - 如何在 iOS 中重新加载我的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849500/

相关文章:

iphone - 更改 iOS 中的地理位置权限通知文本

ios - 从其他类的 super View 中删除 View

ios - 使用自动布局自动调整 UIPopoverController 的大小

iphone - 在iphone中将图像发布到服务器

ios - UICollectionView 上的 UIRefreshControl 高度非常低

iphone - 使用 'presentModalViewController'加载 View

iphone - 未调用核心数据验证方法 - iPhone

ios - imo、whatsapp 和 skype 等 iPhone 锁定时如何实现应用程序调用?

iphone - 在 iOS 中返回随机图像

ios - UISearchBar 上方的线