简体   繁体   中英

UIImageView showing the image only after changing tabs in the UITabBarViewController

I have a UIScrollViewController that has a UIScrollView which holds an UIImageView inside. In loadView of the the scroll view controller I download the image by calling a function, that's supposed to do the downloading on a different thread. I call this function from my loadView and then put the imageview inside the scrollview, and set the scrollview as the view of the controller.

The problem is I can't see the image when I run the program, after clicking a row in the tableView (which is supposed to push the scrollview with the image in it). However, if I change tabss (in the tabbarviewcontroller) and come back to this tab. The image will show.

So I think the image download happens, but I somehow have a problem showing it instantly on the screen. It only appears after I come back to it. What do I seem to be doing wrong? I'm new to threads so I suspect it's a problem with that. Also my code was working before I made it so that it would do the download in another thread, so I'm pretty sure it is related to that.

This is the function in the Photo.m which is an Entity in Core Data. This is supposed to do the download

- (void)processImageDataWithBlock:(void (^)(NSData *imageData))processImage { 
    NSString *url = self.imageURL;
dispatch_queue_t callerQueue = dispatch_get_current_queue();   
dispatch_queue_t downloadQueue = dispatch_queue_create("Flickr download", NULL); 
dispatch_async(downloadQueue, ^{
    NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:url];

    dispatch_async(callerQueue, ^{

        processImage(imageData);   

    });

});

}

This is my loadView method in the PhotoScrollViewController.m

- (void)loadView {

    [image processImageDataWithBlock:^(NSData *imageData) {

        UIImage *imageToBeShown = [UIImage imageWithData:imageData];        


        imageView = [[UIImageView alloc] initWithImage:imageToBeShown];
        CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
        scrollView = [[UIScrollView alloc] initWithFrame:applicationFrame];
        scrollView.delegate = self;
        scrollView.contentSize = imageToBeShown.size;

        [scrollView addSubview:imageView];
        self.title = image.title;
        self.view = scrollView;
    }];
      }

Edited to add extra information that was added as an answer

Adding [super loadView] to my loadView method solved the issue. However, the documentation says that I shouldn't be calling super loadView from my loadView.

I tried moving the code to viewDidLoad and that works as well. But really this is the code that's setting the view so I feel like I should be putting it in loadView. But then it doesn't work when I use this multi threading mechanism for download.

Is this because the download is somehow interfering with me setting the view in loadView?

It seems loadView completes before the image download is done. You need some way of calling [imageView setNeedsDisplay] when image download is complete. You need some method that gets called when image download is done that accesses the imageView (perhaps using viewWithTag) from the scrollView and calls [imageView setNeedsDisplay];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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