【问题标题】:How to show the image via http url in AlertView in Objective-C?如何在 Objective-C 的 AlertView 中通过 http url 显示图像?
【发布时间】:2015-04-10 04:50:35
【问题描述】:

我正在使用 Objective-C 进行开发,我想通过 http urlAlertView 中显示图像?

我已经得到了像:http://192.72.1.1/DCIM/100__DSC/SNAP0053.JPG这样的图片网址,并将NSURL转换为NSString。但我无法在 AlertView 中显示图像,它是空的。

将url转换为NSString并显示在AlertView中的代码如下:

NSString *path = [url absoluteString];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
UIImage *Image=[UIImage imageWithData:imageData];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UIImageView *imageView = [[UIImageView alloc] init];

[imageView setImage:Image];

[alert addSubview:imageView];
[alert show];

但是 AlertView 没有显示如下图所示的任何图像。

我错过了什么吗?

如何在 Objective-C 的 AlertView 中通过 http url 显示图片?

【问题讨论】:

  • 自 iOS7 起,您可以将子视图添加到 UIAlertView(如果我没有记错的话)。您必须使用自定义 UIAlertView(GitHub 上的许多项目都具有相同的行为)
  • 试过这个stackoverflow.com/a/2323694?我认为你没有设置它的 x,y,width 和 height 属性

标签: ios objective-c uiimage uialertview nsurl


【解决方案1】:

您不能在 AlertView 中添加图像。 在 iOS 7 之前这是可能的。 您可以创建自定义 AlertView 并向其添加图像。

也可以看看这个

https://github.com/wimagguc/ios-custom-alertview

【讨论】:

    【解决方案2】:

    不,你不能那样做。用UIView 编写一个自定义控件,模仿UIAlertView 的行为。

    一些建议:

    1. 切勿在UIAlertView 上使用addSubView
    2. UIAlertView 已弃用 UIAlertController

    参考UIAlertView Class Reference

    1)

    UIAlertView 类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的,并且 不得修改。

    2)

    重要提示:UIAlertView 在 iOS 8 中已弃用。(请注意 UIAlertViewDelegate 也已弃用。)创建和管理警报 在 iOS 8 及更高版本中,使用 UIAlertController 和 UIAlertControllerStyleAlert 的首选样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 2012-10-14
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2012-06-20
      相关资源
      最近更新 更多