UIImageView.image没有收到UIImage的值

时间:2014-10-09 01:14:16

标签: ios objective-c uiimageview uiimage

我很难将图像的值分配给图像视图图像。我已经测试过,源图像在分配给图像视图图像时有一个值,实际上确实如此。但是,分配似乎不会发生,并且图像视图图像保持为空。任何人都能帮我解决我做错的事吗?

感谢。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView.contentSize = self.imageView.image.size;
    self.scrollView.delegate = self;
    self.scrollView.minimumZoomScale = 1.0;
    self.scrollView.maximumZoomScale = 100.0;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{

        NSString *imageName = @"http://my.domain.com/image.png";
        UIImage *img = [[UIImage alloc] init];
        NSURL *imageURL = [NSURL URLWithString:imageName];

        [self.activityIndicator startAnimating];

        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        img = [UIImage imageWithData:imageData];

        dispatch_async(dispatch_get_main_queue(), ^{

            self.imageView.image = img;//img HAS a value, but self.imageView.image does NOT receive it

            [self.activityIndicator stopAnimating];
            [self.imageView setNeedsDisplay];
        });
    });
}

3 个答案:

答案 0 :(得分:0)

我认为问题UIImage。在哪里分配UIImage的对象。试试这个:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{

   [self.activityIndicator startAnimating];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://techbeasts.com/wp-content/uploads/2014/04/Wallpaper-HD.jpg"]];  // Your image URL. First check it in browser image is there.

    dispatch_async(dispatch_get_main_queue(), ^{

        self.imageView.image = [UIImage imageWithData:imageData];//img HAS a value, but self.imageView.image does NOT receive it
        [self.activityIndicator stopAnimating];
        [self.imageView setNeedsDisplay];
    });
});

答案 1 :(得分:0)

问题在于,您必须使用dispatch_asyncdispatch_sync对。因此内部块将等待外部块完成其执行并下载图像。

   dispatch_async(queue, ^{

        NSString *imageName = @"http://my.domain.com/image.png";
        UIImage *img = [[UIImage alloc] init];
        NSURL *imageURL = [NSURL URLWithString:imageName];

        [self.activityIndicator startAnimating];

        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        img = [UIImage imageWithData:imageData];

        dispatch_sync(dispatch_get_main_queue(), ^{

            self.imageView.image = img;//img HAS a value, but self.imageView.image does NOT receive it

            [self.activityIndicator stopAnimating];
            [self.imageView setNeedsDisplay];
        });
    });

然后确保通过编程方式或使用IB分配imageView

答案 2 :(得分:0)

插座没有正确连接。重新连接它解决了这个问题。