0

我正在尝试使用以下代码在我的 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
4

3 回答 3

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

希望如此,这段代码可以帮助你。

于 2013-08-09T07:03:29.193 回答
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    
于 2013-08-09T06:53:52.153 回答
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
}
于 2013-08-09T06:55:19.390 回答