yoyosir
yoyosir

Reputation: 458

How to reload my view in iOS

I have a imageView embedded in a scrollView. The imageView needs to be downloaded from the Internet (through flickr api), so I add a thread to handle this. But what am I supposed to do after I finished downloading the image? How can I reload my imageView?Here's my code.

In fact, it is working fine. but the imageView's size is (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];
}

Upvotes: 2

Views: 13762

Answers (1)

user1118321
user1118321

Reputation: 26345

The usual way is to call:

[self.imageView setNeedsDisplay];

Edited to Add:

I just realized you said the image view is still (0,0). From 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.

Upvotes: 1

Related Questions