奇怪的UIImageview远程图像加载行为

时间:2013-08-09 06:38:16

标签: iphone ios ios5

我正在尝试使用以下代码在我的imageview上加载远程图像。 displayImage在testview.xib上用imageview引用

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.displayImage.userInteractionEnabled = YES;

    NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:self.gImg.URL];

    NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];

    self.originalImage = [UIImage imageWithData:imageData];
    self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
    self.displayImage = [[UIImageView alloc] initWithImage:self.originalImage];
    [self.displayImage setImage:self.originalImage];

    NSLog(@"DisplayImage Object:%@",self.displayImage);

    NSLog(@"height of Displayimage image:%.f",self.displayImage.image.size.height);
}

但图像没有显示。我确定图像确实加载在imageview上。这是输出日志:

DisplayImage Object:<UIImageView: 0x11033270; frame = (0 0; 720 632); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1104c620>>
height of Displayimage image:632

3 个答案:

答案 0 :(得分:2)

这里我修改了你的代码: 如果在.xib中有 UIImageView,则无需在代码中分配UIImageView。

NSURL *gImg = [NSURL URLWithString:@"http://www.vinfotech.com/sites/default/files/iphoe-app-design-ios7-way-12.jpg"];
    NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:gImg];

    NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];

         UIImage *originalImage = [UIImage imageWithData:imageData];
          self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
        //  self.displayImage = [[UIImageView alloc] initWithImage:originalImage];
                 [self.displayImage setImage:originalImage];

         NSLog(@"DisplayImage Object:%@",self.displayImage);    
                 NSLog(@"height of Displayimage image:%.f",self.displayImage.image.size.height);

希望如此,此代码可以帮助您。

答案 1 :(得分:1)

创建一个调度队列:

dispatch_queue_t  backgroundQueue = dispatch_queue_create("com.mayapp", NULL);        

使用NSData方法dataWithContentsOfURL并在后台主题中下载图片:

    dispatch_async(backgroundQueue, ^(void) {

        NSData *data = [NSData dataWithContentsOfURL:requestWithBodyParams];
        //as soon as the image is downloaded, draw image in a main thread
    dispatch_sync(dispatch_get_main_queue(), ^(void) {
        self.originalImage = [UIImage imageWithData:data];

        self.displayImage.contentMode = UIViewContentModeScaleAspectFit;
        self.displayImage = [[UIImageView alloc] initWithImage:self.originalImage];
        [self.displayImage setImage:self.originalImage];

});//close main block    
});//background block    

答案 2 :(得分:1)

此示例代码适用于我:

- (void)viewDidLoad
{
    [super viewDidLoad];

   NSURL *url = [NSURL URLWithString:@"http://iceclearmedia.com/wp-content/uploads/2011/04/SEO-and-url-Shorteners.jpg"];
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:url];
    UIImage* image = [[UIImage alloc] initWithData:imageData] ;

    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
}

- (void)displayImage:(UIImage *)image {
    [self.imagedownload setImage:image]; //UIImageView
}