简体   繁体   English

UIImageView仅在更改UITabBarViewController中的选项卡后才显示图像

[英]UIImageView showing the image only after changing tabs in the UITabBarViewController

I have a UIScrollViewController that has a UIScrollView which holds an UIImageView inside. 我有一个UIScrollViewController,它具有一个UIScrollView,其中包含一个UIImageView。 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. 在滚动视图控制器的loadView中,我通过调用一个函数来下载图像,该函数应该在另一个线程上进行下载。 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. 我从loadView调用此函数,然后将imageview放在scrollview内,并将scrollview设置为控制器的视图。

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). 问题是,在运行程序时,在tableView中单击一行后,我看不到该图像(应该将其中包含图像的scrollview推入其中)。 However, if I change tabss (in the tabbarviewcontroller) and come back to this tab. 但是,如果我更改了选项卡(在tabbarviewcontroller中)并返回到此选项卡。 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. 这是Photo.m中的功能,它是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 这是我在PhotoScrollViewController.m中的loadView方法

- (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. 将[super loadView]添加到我的loadView方法中可以解决此问题。 However, the documentation says that I shouldn't be calling super loadView from my loadView. 但是,文档说我不应该从我的loadView调用super loadView。

I tried moving the code to viewDidLoad and that works as well. 我尝试将代码移至viewDidLoad,效果也很好。 But really this is the code that's setting the view so I feel like I should be putting it in loadView. 但这确实是设置视图的代码,因此我觉得应该将其放入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? 这是因为下载会以某种方式干扰我在loadView中设置视图吗?

It seems loadView completes before the image download is done. 似乎loadView在映像下载完成之前已完成。 You need some way of calling [imageView setNeedsDisplay] when image download is complete. 图像下载完成后,您需要某种方式调用[imageView setNeedsDisplay]。 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]; 您需要一种在完成图像下载后会调用的方法,该方法可从scrollView访问imageView(也许使用viewWithTag)并调用[imageView setNeedsDisplay]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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