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

标签 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

最佳答案

这里我修改了你的代码: 如果您在 .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);

希望如此,这段代码对您有所帮助。

关于iphone - 奇怪的 UIImageview 远程图像加载行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18141287/

相关文章:

ios - 核心数据在更新时应用删除规则

ios5 - iPhone 模拟器中的 VPN 连接

uitableview - UITableView iOS 5顶部的灰色线

ios - 在 IOS 7 中将状态栏颜色设置为黑色

iphone - 防止 subview 在 UIScrollView 中滚动

iphone - 是否可以减慢scrollRangeToVisible的速度?

iphone - 使用 xcode 4.2 和 Storyboard 通过代码更改 View ?

iphone - 在 GDB 中访问静态方法

iOS 7 条形按钮项目 : custom background images and highlight state darkens image

ios - 如何在特定时间后取消 iOS 本地通知?