【问题标题】:Xcode iPhone Programming: Loading a jpg into a UIImageView from URLXcode iPhone 编程:从 URL 将 jpg 加载到 UIImageView
【发布时间】:2011-01-31 09:35:45
【问题描述】:

我的应用必须从 http 服务器加载图像并将其显示到 UIImageView
我该怎么做??
我试过这个:

NSString *temp = [NSString alloc];
[temp stringwithString:@"http://192.168.1.2x0/pic/LC.jpg"]
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
    nil,
    (CFStringRef)temp,                     
    NULL,
    NULL,
    kCFStringEncodingUTF8)
autorelease];


NSData *dato = [NSData alloc];
 dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
 pic = [pic initWithImage:[UIImage imageWithData:dato]];

此代码在视图的 viewdidload 中,但没有显示任何内容! 服务器正在工作,因为我可以从中加载 xml 文件。但我无法显示该图像!
我需要以编程方式加载图像,因为它必须根据传递的参数而改变! 先感谢您。 安东尼奥

【问题讨论】:

  • 仅供参考:您不需要在帖子中添加
    标签
  • 您的代码看起来很奇怪,stringwithString: 是一个类方法,所以基本上我认为 [temp stringwithString:@"192.168.1.2x0/pic/LC.jpg"] 无效。您是否尝试仅使用 dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"192.168.1.2x0/pic/LC.jpg"]] ?
  • 假设 pic 是您班级中的一张图片 - 是您想要做的作业吗? (pic = [pic initWithImage...])
  • 您似乎真的误解了 Objective-C 中对象创建的基础知识。我建议阅读 Apple 的介绍性材料,例如 The Objective-C Programming Language,尤其是关于对象创建和初始化的章节:developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/…
  • 其他人已经回答了大部分问题,但我相当肯定 192.168.1.2x0 不是有效的 IP 地址。

标签: iphone objective-c xcode url uiimageview


【解决方案1】:

应该是:

NSURL * imageURL = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];

一旦你有了image 变量,你就可以通过它的image 属性把它扔到UIImageView 中,即:

myImageView.image = image;
//OR
[myImageView setImage:image];

如果您需要转义原始字符串中的特殊字符,您可以这样做:

NSString * urlString = [@"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
....

如果您以编程方式创建 UIImageView,您可以这样做:

UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[someOtherView addSubview:myImageView];
[myImageView release];

【讨论】:

  • 非常感谢我不知道 UIImageView 是如何工作的!就是这么简单!非常感谢!
  • 警告,dataWithContentsOfURL: 不是异步的……你可以在这里阻塞主线程吧?
  • 我发现从 url 加载的图像与使用 [self.imageView setImage: [UIImage imageNamed:@"AKL.JPG"]] 本地加载的相同图像相比显得模糊;
  • 这不是后台任务,会使视图冻结。您应该使用块或 NSThread 在后台加载图像。
【解决方案2】:

因为从 URL 加载图像需要很长时间,所以最好异步加载它。以下指南对我来说非常简单:

http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation

【讨论】:

  • 为了记录,这仍然需要“花费很长时间”,用户界面不会锁定。
【解决方案3】:

试试这个

NSURL *url = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
 NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]]; 
[cell addSubview:subview];
[subview release];

一切顺利。

【讨论】:

    【解决方案4】:

    这是实际的代码。

    UIImageView *myview=[[UIImageView alloc]init];
    
        myview.frame = CGRectMake(0, 0, 320, 480);
    
        NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];
    
        NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];
    
        UIImage *image=[[UIImage alloc]initWithData:imgdata];
    
        myview.image=image;
    
        [self.view addSubview:myview];
    

    【讨论】:

      【解决方案5】:

      您应该在背景中加载图像,否则视图将冻结。 试试这个:

      UIImage *img = [[UIImage alloc] init];
      
      dispatch_async(dispatch_get_global_queue(0,0), ^{
      
          NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.test.com/test.png"];
          img = [UIImage imageWithData: data];
      
           dispatch_async(dispatch_get_main_queue(), ^{
              //PUT THE img INTO THE UIImageView (imgView for example)
              imgView.image = img;
           });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多