繁体   English   中英

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

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

我有一个UIScrollViewController,它具有一个UIScrollView,其中包含一个UIImageView。 在滚动视图控制器的loadView中,我通过调用一个函数来下载图像,该函数应该在另一个线程上进行下载。 我从loadView调用此函数,然后将imageview放在scrollview内,并将scrollview设置为控制器的视图。

问题是,在运行程序时,在tableView中单击一行后,我看不到该图像(应该将其中包含图像的scrollview推入其中)。 但是,如果我更改了选项卡(在tabbarviewcontroller中)并返回到此选项卡。 图像将显示。

因此,我认为图像下载发生了,但是以某种方式在屏幕上立即显示它却有问题。 它仅在我回到它之后出现。 我似乎在做什么错? 我是线程的新手,所以我怀疑这是一个问题。 同样,我的代码在我实现之前就已经工作了,以便可以在另一个线程中进行下载,因此我很确定它与此相关。

这是Photo.m中的功能,它是Core Data中的一个实体。 这应该做下载

- (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);   

    });

});

}

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

编辑以添加作为答案添加的其他信息

将[super loadView]添加到我的loadView方法中可以解决此问题。 但是,文档说我不应该从我的loadView调用super loadView。

我尝试将代码移至viewDidLoad,效果也很好。 但这确实是设置视图的代码,因此我觉得应该将其放入loadView中。 但是,当我使用这种多线程机制进行下载时,它不起作用。

这是因为下载会以某种方式干扰我在loadView中设置视图吗?

似乎loadView在映像下载完成之前已完成。 图像下载完成后,您需要某种方式调用[imageView setNeedsDisplay]。 您需要一种在完成图像下载后会调用的方法,该方法可从scrollView访问imageView(也许使用viewWithTag)并调用[imageView setNeedsDisplay]。

暂无
暂无

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

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