2

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];
}
2
  • Does it work if you comment out dispatch_release(downloadQueue);? I think you are releasing the dispatch queue before the block gets executed. Commented Mar 24, 2012 at 6:01
  • tried that. still not working.. Commented Mar 24, 2012 at 6:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that. I replaced [self viewWillAppear:NO]; with [self.imageView setNeedsDisplay]; but it doesn't work.
Is anything written to the console? Also, are you sure it's executing that line? Can you stop on it in the debugger?
2012-03-24 14:13:49.451 TopPlaces[1511:f803] imgview320, 367 2012-03-24 14:13:49.452 TopPlaces[1511:f803] img0, 0
it is executing because i got a spinning wheel on the top right.
in fact my problem is that my view size is strange. the image view is of the size(0,0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.